note 79431 deleted from language.constants by cmb

From: Date: Tue, 05 Jul 2016 12:55:09 +0000
Subject: note 79431 deleted from language.constants by cmb
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-205883@lists.php.net to get a copy of this message
Note Submitter: ben at bendodson dot com ---- I recently found I needed a way of retrieving the value of a constant dynamically - e.g. trying to find the value of FOO_BAR by passing 'FOO_' . $someVariableWithValueBAR. I came up with the following solution: <?php define('FOO_BAR','It works!'); define('FOO_FOO_BAR','It works again!'); // prints 'It works!' $changing_variable = 'bar'; echo constant('FOO_' . strtoupper($changing_variable)); // prints 'It works again!' $changing_variable = 'foo_bar'; echo constant('FOO_' . strtoupper($changing_variable)); ?> Note the use of strtoupper() as constants should be defined in uppercase for good practice - feel free to remove if you have constants defined in lowercase or you can set $changing_variable as uppercase. Might be of some use to someone!

« previous php.notes (#205883) next »