Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get constant value by variable value

Tags:

php

I need to fetch a constant value. The constant name is stored in a variable.

<?php

define('SomeConstant', 12345);

$variable = 'SomeConstant';

?>

How would I echo "12345" using the variable?

like image 286
nick Avatar asked Mar 01 '26 13:03

nick


2 Answers

Use the constant function

echo constant($variable);
like image 187
Bugs Avatar answered Mar 04 '26 03:03

Bugs


<?php

define('SomeConstant', 12345);

$variable = SomeConstant;

echo $variable;

?>
like image 37
Rich Bradshaw Avatar answered Mar 04 '26 01:03

Rich Bradshaw