PHP Constants
ADVERTISEMENTS
PHP constant is an identifier for simple value or except a variable once. It can not change or undefined during the execution of the script and It executes across the script.
-
A constant is case-sensitive by default.
-
The constant identifier is always uppercase.
-
A valid constant name of PHP starts with a letter or underscore.
-
A constant followed by any number of letters, numbers, or underscores.
How to create a PHP constant?
To create a constant, You will have to use the define function.
How to declaration constant in PHP?
define (name, value, case-insensitive)
Take an example to declare the constant
<?php
// These are the valid constant names
define("TEXT", "hello php program");
define("FOO_BAR", "something more");
define("__FOO__", "something");
echo TEXT . "\n\n";
echo FOO_BAR . "\n\n";
echo __FOO__ . "\n\n";
// These are the invalid constant name
define("2FOO", "something");
?>
Output:
hello php program
something more
something
Is PHP constant global?
The answer is Yes!
The PHP constants are automatically global. So you can use this across the entire script and also inside a local function.
<?php
define("TEXT", "hello php program");
function Test()
{
echo TEXT . "\n";
}
Test();
?>
Output:
hello php program