PHP Constants

Constants are similar to the variable except once they defined, they can never be undefined or changed. A constant value cannot change during the execution of the script. Once a constant is defined, it cannot be undefined or redefined.

Generally, PHP constants are defined in uppercase letters.

PHP constants follow the same PHP variable naming rules. A valid constant name starts with a letter or underscore (no $ sign before the constant name). Constants are case-sensitive.

Unlike variables, constants are automatically global across the entire script.

The define() function in PHP is used to create a constant.

PHP constant: define()

Use the define() function to create a constant. It defines constant at run time.

Syntax

define(ConstantName, value, case-insensitive)
  • ConstantName: It specifies the constant name.
  • value: The value to be stored in the constant.
  • case-insensitive: Specifies whether a constant is case-insensitive. Default value is false. It means it is case sensitive by default.

Example

<?php  
define("MESSAGE","Hello The New Tutorial PHP<br>");  
echo MESSAGE;  

//Create a constant with case-insensitive name:
define("MESSAGE1","Hello Hello The New Tutorial.com PHP",true);//not case sensitive    
echo MESSAGE1, "</br>";    
echo message1;  
?>
PHP Constants Example Output