PHP constants

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.

Need of constants

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 define constant

PHP have a set of functions to handle constants. They are explained below.

define(): define a constant

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.

defined(): check whether a named constant is already defined or not

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.

constant(): Retrieve the value of a named 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.

Code | Download
<?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.

Use of PHP constant

Although there is no such rule where you should use constants in writing PHP script but very common field of applications are,

  • Defining language of installation: multi-language supported scripts use named constants throughout the script and during installation they request installer the preferred language such that they can include the appropriate language files which actually define the values of the constants used in the script code.
  • Domain name and path where the script is being installed: a script may be installed in different domains. So script writer uses named constants for domain name and path where the script is being installed. The values of those constants are collected and defined during installation.
  • The script supported by remote database generally define database user name, password, database name, host name, table prefix as constant such that they are globally available within the script code.
  • Client IP address and country;
  • The timestamp when the script page is requested by the client.
Comments:

Add comment