Constants, by definition, are variables which cannot be modified during the execution of the script. Constants are typically recognizable by their recommended naming convention of ALL_CAPS.
Syntax[edit | edit source]
A coder can only define a constant with the define() function. Following the API for this function, we find that constants can be defined like this:
<?php define('CONSTANT_NAME', 'The Value', true); ?>
The first parameter is the name of the constant, the second parameter is the value the constant represents, and the optional third parameter accepts a boolean TRUE or FALSE to determine whether or not usage of the constant is case insensitive. If the third parameter is omitted, the default is FALSE, causing the variable to be case sensitive. The value of a constant can only be a string, integer, boolean or a float. Constants are accessed just like variables, e.g:
<?php define('CONSTANT', 'Hello'); echo CONSTANT; // Would echo 'Hello' ?>
Usage[edit | edit source]
Constants may just seem like glorified variables to some, but they may be useful at points, if only for the fact they are more secure and are global in scope (they can be accessed anywhere in the script).
There are a large number of pre-defined constants, that can be viewed here: http://www.php.net/manual/en/reserved.constants.php