Constants are like variables except that once they are defined they cannot be changed or undefined.
PHP Constants
A constant is an identifier (name) for a simple value. The value cannot be changed during the script.
A valid constant name starts with a letter or underscore (no $ sign before the constant name).
Note: Unlike variables, constants are automatically global across the entire script.
Set a PHP Constant
To set a constant, use the define() function – it takes three parameters: The first parameter defines the name of the constant, the second parameter defines the value of the constant, and the optional third parameter specifies whether the constant name should be case-insensitive. Default is false.
The example below creates a case-sensitive constant, with the value of “Welcome to vshakya.in!”:
Example
<?php
|
|
define("GREETING", "Welcome to vshakya.in!"); |
|
echo GREETING;
|
|
?>
|
The example below creates a case-insensitive constant, with the value of “Welcome to vshakya.in!”:
Example
<?php
|
|
define("GREETING", "Welcome to vshakya.in!", true); |
|
echo greeting;
|
|
?>
|