Define constants in PHP is useful for us to be able to reuse its content in different instances of code execution PHP. Since we know that the content of the constant will not change throughout the execution of the program.
The definition of constants in PHP is done by using the define function, its syntax is the following:
define(__CONSTANT_NAME_,_CONSTANT_VALUE_);
The names of the constants in PHP must begin with a letter or underscore, followed by letters, numbers or underscores.
By convention, the names of constants in PHP, they are usually defined in capital letters.
Next we see how we have defined a few constants in PHP using the define statement:
define("PATH","/directory/subdirectory/"); // defining path to current directory
define("PI",3.141592); // defining a numerical constant
define("FLAG",TRUE); //defining a flag
define("BR","<br>"); //defining an html line break
To make use of the constants in PHP are only used depending on the names of the constants as follows:
echo PATH;
echo PI*$radius;
echo FLAG;
That is, we just have to put the name of the constant in PHP.
We hope you liked it and, above all, that the article helps you define the constants in PHP.
