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

<?

$name = 'MYCONSTANT';
$value = 100;

define($name, $value);

$return = constant($name);

echo $return;

?>
100

2

<?

define('MYCONSTANT', 100);

echo constant('MYCONSTANT');

?>
100

3

<?

define('MYCONSTANT', 100);

echo MYCONSTANT;

?>
100

4

<?

const MYCONSTANT = 100;

echo constant('MYCONSTANT');

?>
100

5

<?

const MYCONSTANT = 100;

echo MYCONSTANT;

?>
100

6

<?

interface myinterface
{
    const MYCONSTANT = 'some';
}
class myclass
{
    const MYCONSTANT = 'test';
}

echo constant('myinterface::MYCONSTANT') . PHP_EOL;
echo constant('myclass::MYCONSTANT');

?>
some
test

7

<?

interface myinterface
{
    const MYCONSTANT = 'some';
}
class myclass
{
    const MYCONSTANT = 'test';
}

$name = 'MYCONSTANT';

echo constant('myinterface::' . $name) . PHP_EOL;
echo constant('myclass::' . $name);

?>
some
test

PHP

Home Menu