Tuesday, April 22, 2025

PHP Constants

You are advised to check corresponding YouTube video at the end of this article.

Constants are fixed values that do not change during the execution of a program. Once defined, a constant retains its value throughout the entire program and cannot be modified by the program or the user.

Constants are useful in situations where you need to use a value that remains the same throughout the program, such as mathematical constants (e.g., pi), physical constants (e.g., speed of light), or configuration values (e.g., database credentials).

In many programming languages, constants are typically defined using the const or define keyword and are given a name and a value. Once defined, the constant can be used throughout the program using its name, and its value cannot be changed or modified. 


<?php 

define("SYSTEM_ver", "SAP v0.5");
define("LEGAL", "Please, read TOS");
define("NOTICE", "You will be kicked after 3 failed logins");
define("HR", "<hr>");

echo SYSTEM_ver;
echo HR;

echo LEGAL;
echo HR;

echo NOTICE;
echo HR;

?>

This code defines four constants using the define() function and then uses them to output messages with a horizontal line separator between them.

The first constant is SYSTEM_ver with the value of "SAP v0.5", which could be used to identify the version number of the system. The second constant is LEGAL with the value of "Please, read TOS", which could be used to display a legal notice or terms of service. The third constant is NOTICE with the value of "You will be kicked after 3 failed logins", which could be used to warn users about login attempts.

The last constant is HR with the value of "<hr>", which is an HTML horizontal line element. This constant is used to separate each message with a horizontal line, making the output more readable. 


<?php 

define("SYSTEM_ver", "SAP v0.5");
define("LEGAL", "Please, read TOS");
define("NOTICE", "You will be kicked after 3 failed logins");
define("HR", "<hr>");

function message() {
	echo SYSTEM_ver;
	echo HR;

	echo LEGAL;
	echo HR;

	echo NOTICE;
	echo HR;
}

message();

?>

Explanation:

  • The define() function is used to define constants in PHP.
  • In this example, we define four constants: SYSTEM_ver, LEGAL, NOTICE, and HR.
  • We then create a function message() that simply echoes these constants.
  • Finally, we call the message() function, which outputs the values of the constants with horizontal lines in between them.

No comments:

Post a Comment

Tkinter Introduction - Top Widget, Method, Button

First, let's make shure that our tkinter module is working ok with simple  for loop that will spawn 5 instances of blank Tk window .  ...