Constants are data which do not change their values. 5, 5.6, "This is a text", are examples of integer, float and string constants. Named constants are constants which are defined by a unique name and they hold constant values through out the execution of a program.
PHP allows programmers to define named constants. Only scalar data (array are vector data) may be defined as constants. Scalar values are integer, float, string or Boolean values. The scope of a constant is global, i.e. wherever you define them they can be accessed through out the program code. And once defined you can't unset or redefine a constant.
Now the question is as constants are assigned to fixed scalar values and we can't redefine them so why we need constants; why we do not put their values instead? The answer is simple; the difference between an absolute constant and named constant is that a named constant remains constant only within the execution of program code. At a new instant of program it may assigned to a new value. For example the IP address of the client computer is constant while server is executing the PHP code, but this value must vary from connection to connection requesting the script page from the server. There are other several factors for which constants may be assigned to variable quantities. But an absolute constant like 5, does not change its value. So we can conclude that the named constants are constants only within the scope of a program instant.
PHP have a set of functions to handle constants. They are explained below.
Syntax of define(): bool define( string $name , mixed $value [, bool $case_insensitive = false ] )
The define function takes three arguments; name of the constant, its value and whether that name is case sensitive.
Syntax of defined(): bool defined ( string $name)
This function takes one argument, name of the constant and returns true if the constant is already defined or false if not defined. This checking is necessary since you can't re-define a PHP constant.
Syntax of constant(): mixed constant (string $name)
This function takes only one argument, name of the constant and returns its value on success or NULL if there is no such constant defined with this name. In such case an E_WARNING level error is generated if the constant is not defined.
In general you can simply put the constant name to retrieve its value, but this function is necessary when you do not know the name of the constant, i.e. when the constant name had came from the return value of a function during its declaration.
From PHP version 5.3.0 the keyword const can be used to define a constant. const keyword must be declared at the top level of the code, i.e. outside functions, loops or if statements or a class definition.
<?php
//Code examples of handlling constants.
define('DOMAIN','ramui.com');//Case sensitive constant.
echo DOMAIN;//Out-put is: ramui.com
define('DOMAIN','google.com');//Generate error since 'DOMAIN' is already defined.
if(!defined('DOMAIN')){define('DOMAIN','ramui.com');}
//The above statement will do nothing since the constant 'DOMAIN' is already defined.
define('AUTHOR','Archan Ghosal',true);//Case insensitive constant.
echo author;//Out-put is: Archan Ghosal.
echo constant('author');//Same as above.
define('START_TIME',time());//Here the constant 'START_TIME' is assigned to a value from another PHP function.
const AUTHOR='Archan Ghosal';//Can be used from and above PHP version 5.3.0
?>
The complete reference of those functions and keyword can be obtained from the PHP documentation available at the official website of PHP.
Although there is no such rule where you should use constants in writing PHP script but very common field of applications are,