☰ See All Chapters |
PHP Constants
PHP constants are like variables except that once they are initialized they cannot be changed. A constant name can start with a letter or underscore (no $ sign before the constant name). The convention is to use UPPERCASE letters for constant names. PHP constants can be defined by 2 ways:
Using define() function
Using const keyword
define() function syntax
define("CONSTANT_NAME", value); // boolean
const keyword syntax
const CONSTANT_NAME = value; // integer
Examples
<?php const HUNDRED = 100; // integer define("TWO_IS_ODD_NUMBER", false); // boolean const UNKNOWN = null; // null define("FULL_NAME", "Manu Manjunatha"); // string const PI = 22/7; // float, using expressions const PROGRAMMING_LANGUAGES = ["Java", "PHP", "Javascript", "C++", "Python", "Perl"]; // arrays define("FAVOURITE_PROGRAMMING_LANGUAGES", ["Java", "PHP", "Python"]); // arrays const TWO_IS_EVEN_NUMBER = !TWO_IS_ODD_NUMBER; // Define constant using another constant define("NULL_VALUE", UNKNOWN); // Define constant using another constant ?> |
When you define constant with same name which is already defined you will get the error as below
<?php const TWO_IS_EVEN_NUMBER = true; define("TWO_IS_EVEN_NUMBER", true); echo TWO_IS_EVEN_NUMBER; ?> |
If you are not sure if constant is already defined then below syntax will define constant if it's not yet defined. In this syntax we use defined keyword (note the difference between define and defined)
<?php const TWO_IS_EVEN_NUMBER = true; defined("TWO_IS_EVEN_NUMBER") || define("TWO_IS_EVEN_NUMBER", true); // "define PI if it's not yet defined" echo TWO_IS_EVEN_NUMBER; ?> |
Reserved constants
Some constant names are reserved by PHP and cannot be redefined. A Notice will be issued as “Constant ... already defined in ...” All the below examples will fail:
<?php define("true", false); define("false", true); define("CURLOPT_AUTOREFERER", "something"); define("null", null); ?> |
const vs define
define() is a runtime expression while const a compile time one. We can't use the const keyword to declare constant in conditional blocks, while with define() we can achieve that. You can use any expression to both const and define but the expression for const should be evaluated to a value during compile time itself whereas define() takes any expression which can be evaluated to a value during runt time also. const can also be utilized within a class or interface to declare a class constant or interface constant, while define() can't be utilized within a class or interface.
Checking if constant is defined
To check if constant is defined use the defined function. Note that this function doesn't care about constant's value, it only cares if the constant exists or not. Even if the value of the constant is null or false the function will still return true. Note that constant becomes "visible" in your code only after the line where you have defined it.
<?php define("HUNDRED", 100); if (defined("HUNDRED")) { echo "HUNDRED is defined </br> "; }
if (! defined("HUNDRED")) { define("HUNDRED", 100); }
if (defined("TEN")) { echo "TEN is defined"; // doesn't print anything, TEN is not defined yet. } define("TEN", 100); if (defined("TEN")) { echo "TEN is defined"; }
?> |
Getting all defined constants
get_defined_constants() function returns all the constants including predefined constants of PHP. To get the constants defined by your code you have to call the get_defined_constants() function twice. One at the beginning and another at the place where you need the array of user defined constants and take the difference of these two.
<?php $constants = get_defined_constants(); define("TEN", 10); define("HUNDRED", 100); $new_constants = get_defined_constants(); $myconstants = array_diff_assoc($new_constants, $constants); var_export($myconstants);
?> |
All Chapters