PHP Constants

Register here to save your progress

? ?


As the name already says, opposite to the variables, the constant's value is constant, it doesn't change during the application's execution.

To define a constant you need to use the function define().


//we create the constant PI
define("PI", 3.14);

echo PI;
//will output 3.14

In the name of a constant we don't use the symbol $ at the beginning. Also, as a convention among programmers, their names are written with uppercase letters, to make them easier to identify.

Remember to use double quotations marks, "", when defining the name inside the function define().

What function we use to define constants?

In order to check if a constant if defined you can use the function defined. This function will return 1 if the constant has been defined or 0 if it hasn't been defined.


//check if the constant has been defined
echo defined("PI")


What value will output the function defined if the constant has been defined?