Menu Home

constant

osbo.com | PHP | Functions | constant

Returns the value of a constant

Syntax

constant ( string $name ) : mixed

Parameters

name

The constant name.

Return

Returns the value of the constant, or NULL if the constant is not defined.

Examples

1

<?

define("MAX", 100);
echo MAX . PHP_EOL;
echo constant("MAX");

?>
100
100

2

<?

interface myinterface {
    const MYCONSTANT = 'some';
}
class myclass {
    const MYCONSTANT = 'test';
}
var_dump(constant('myinterface::MYCONSTANT'));
var_dump(constant('myclass::MYCONSTANT'));

?>
string(4) "some"
string(4) "test"

3

<?

interface myinterface {
    const MYCONSTANT = 'some';
}
class myclass {
    const MYCONSTANT = 'test';
}
$constant = 'MYCONSTANT';
var_dump(constant('myinterface::' . $constant));
var_dump(constant('myclass::' . $constant));

?>
string(4) "some"
string(4) "test"

PHP