constantReturns the value of a constant.
This function is useful if you need to retrieve the value of a constant, but do not know its name; that is, it is stored in a variable or returned by a function.
This function allows another way to show a CONSTANT value.
If the $name is not defined an E_WARNING level error or NULL is generated.
<?php
mix constant ( str $name )
where,
$name = The constant name
?>
$name
The name of the CONSTANT to print the value.
EXERCISE
<?php
define ('PI', M_PI);
echo 'The value of the defined constant is:<br>';
echo 'PI = ' . constant("PI");
?>
RESULT
The value of the defined
for this INTERNAL constant is:
PI = 3.1415926535898
EXERCISE
<?php
$path = "PATH2VERA";
echo 'The path to the character set VERA is:<br>';
echo constant($path);
?>
RESULT
The path to the character set VERA character set is:
H:\\...\ttf\vera
Constant value assigned to the path to Vera.
Just for example.
EXERCISE
<?php
$arr = [ 1, 2, 3, 'a', 'c'];
echo 'The currently defined constant is:<br>';
var_dump(constant($arr));
?>
RESULT
In PHP 7.4.XX it displays a Warning message, but in PHP 8.0.XX it displays a Fatal error.
EXERCISE
<?php
define('A', 'AÂÂÁÀ');
echo 'The currently defined constant is:<br>';
var_dump(constant('A'));
/* - - - - - - - - - - - - - - - - - - - - -
Accented letters count
as having two characters.
- - - - - - - - - - - - - - - - - - - - - */
?>
EXERCISE
<?php
define ("P205a", $_SERVER['DOCUMENT_ROOT'] . '/adds/');
define ("P205b", 'file://' . $_SERVER['DOCUMENT_ROOT'] . '/adds/');
define ("P205c", $_SERVER['DOCUMENT_ROOT'] . '/temp/');
$arr05 = [ 'P205a', 'P205b', 'P205c' ];
print_r($arr05);
echo '<br><br>' . constant($arr05[0]);
echo '<br><br>' . constant($arr05[1]);
echo '<br><br>' . constant($arr05[2]);
?>
RESULT
Constant values assigned to paths, just for examples.
Test with your own values.