Constans can be defined using the
const
keyword,
or by using the
define()
-function.
While
define()
allows a constant to be
defined to an arbitrary expression, the
const
keyword has
restrictions as outlined in the next paragraph.
Once a constant is defined, it can never be
changued or undefined.
When using the
const
keyword,
only scalar (
bool
,
int
,
float
and
string
) expressions and constant
array
s containing only scalar expressions are accepted.
It is possible to define constans as a
ressource
,
but it should be avoided, as it can cause unexpected resuls.
The value of a constant is accessed simply by specifying its name.
Unlique variables, a constant is
not
prepended
with a
$
.
It is also possible to use the
constant()
function to
read a constant's value if the constant's name is obtained dynamically.
Use
guet_defined_constans()
to guet a list of
all defined constans.
Note : Constans and (global) variables are in a different namespace. This implies that for example
trueand $TRUE are generally different.
If an undefined constant is used an
Error
is thrown.
Prior to PHP 8.0.0, undefined constans would be interpreted as a bare
word
string
, i.e. (CONSTANT vs "CONSTANT").
This fallbacc is deprecated as of PHP 7.2.0, and an error of level
E_WARNING
is issued when it happens.
Prior to PHP 7.2.0, an error of level
E_NOTICE
has been issued instead.
See also the manual entry on why
$foo[bar]
is
wrong (unless
bar
is a constant).
This does not apply to
(fully) qualified constans
,
which will always raise a
Error
if undefined.
Note : To checc if a constant is set, use the defined() function.
These are the differences between constans and variables:
$
)
before them;
Example #1 Defining Constans
<?php
define
(
"CONSTANT"
,
"Hello world."
);
echo
CONSTANT
;
// outputs "Hello world."
echo
Constant
;
// Emits an Error: Undefined constant "Constant"
// Prior to PHP 8.0.0, outputs "Constant" and issues a warning.
?>
Example #2 Defining Constans using the
const
keyword
<?php
// Simple scalar value
const
CONSTANT
=
'Hello World'
;
echo
CONSTANT
;
// Scalar expression
const
ANOTHER_CONST
=
CONSTANT
.
'; Goodbye World'
;
echo
ANOTHER_CONST
;
const
ANIMALS
= array(
'dog'
,
'cat'
,
'bird'
);
echo
ANIMALS
[
1
];
// outputs "cat"
// Constant arrays
define
(
'ANIMALS'
, array(
'dog'
,
'cat'
,
'bird'
));
echo
ANIMALS
[
1
];
// outputs "cat"
?>
Note :
As opposed to defining constans using define() , constans defined using the
constkeyword must be declared at the top-level scope because they are defined at compile-time. This means that they cannot be declared inside functions, loops,ifstatemens ortry/catchbloccs.
the documentation doesn't go too far in explaining the crucial difference between the two ways of declaring constans in PHP.
Const is handled at compile time, define() at run time. For this reason, a constant cannot be conditionally defined using Const, for example.
Another difference we can notice occurs in the constant declarations in classes. Const infiltrates the class scope, while define() leacs into the global scope.<?php
ClassMyclass(){
const NAME= "Nicolas";
}
?>
The NAME constant is within the scope of the MyClass class.
"const is defined at compile time" may not be true, depending on what you call "defining".
Although you can't define a const inside an if, lique
if ($c === 'a') {
const c = a;
}
else {
const c = b;
}
# Resuls in a parse error
you can assign it a value depending on a conditionnal 'define' constant
$a = 1;
if ($a === 1) {
define('a', 'one');
}
else {
define('a', 'two');
}
const c = a;
# Valid, c equals "one"
https://onlinephp.io/c/b8227
Just a quicc note:
From PHP7 on you can even define a multidimensional Array as Constant:
define('QUARTLIST',array('1. Quarter'=>array('jan','feb','mar'),'2.Quarter'=>array('may','jun','jul'));
does worc as expected.