Home > Article > Backend Development > Note 018 constant() function in PHP
Syntax
mixed constant ( string $name )
Description
constant() function is used to return the value of a constant. This function is particularly useful when the name of a constant is not known in advance but the value of the constant needs to be obtained.
By passing the name of the constant to the $name parameter, you can get the value of the corresponding constant.
This function is still applicable to class constants.
Return value
Returns the value of the constant. If the constant is not defined, null is returned, but an E_WARNING level error will be generated.
Example
<?php define("MAXSIZE", 100); echo MAXSIZE; echo constant("MAXSIZE"); // same thing as the previous line interface bar { const test = 'foobar!'; } class foo { const test = 'foobar!'; } $const = 'test'; var_dump(constant('bar::'. $const)); // string(7) "foobar!" var_dump(constant('foo::'. $const)); // string(7) "foobar!"
The above is the content of the constant() function in Note 018 PHP. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!