PHP Constants

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

PHP : Constants

Description

A constant is an identifier for a simple value. The value cannot be modified during the script's execution. A valid constant name starts with a letter or underscore (no dollar($) sign before the name) and by convention constant identifier are always uppercase.

Constants are defined by using the define() function or by using the const keyword outside a class definition as of PHP 5.3.0. The name of the constant and the value must be placed within the parentheses. After defining it can never be changed or undefined. Only scalar data i.e. boolean, integer, float and string can be contained in constants.

PHP valid and invalid constant names

  1. //Valid constants name  

  2. define("COUNTRY_NAME", "India");  

  3. define("CITYNAME", "New Delhi");  

  4. define("2NO_OF_STUDENTS", 60);  

  5. ?>  

Example of a PHP constant

  1. define("COUNTRY_NAME", "India");  

  2. echo COUNTRY_NAME;  

  3. ?>  

Output :

India