A constant is an identifier (name) for a simple value that cannot be changed during the execution of the script.
A valid constant name must begin with a letter or an underscore (without the $ sign).
Note: Unlike variables, constants are automatically global throughout the entire script.
To create a constant, use the define() function.
define( |
Parameters:
Create a constant with a case-sensitive name:
define("GREETING", |
Create a constant with a case-insensitive name:
define("GREETING", |
You can also define a constant using the const keyword.
Create a constant using the const keyword:
const |
|
Since PHP 7, you can create an array constant using the define() function.
Define an array constant:
define("cars", |
Constants are automatically global and can be accessed throughout the entire script.
This example demonstrates using a constant inside a function, even though it is defined outside the function.
define("GREETING", function myTest(); |