note 79431 added to language.constants
| From: | ben at bendodson dot com | Date: | Tue, 27 Nov 2007 11:14:57 +0000 |
| Subject: | note 79431 added to language.constants | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-132590@lists.php.net to get a copy of this message | ||
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!
----
Server IP: 87.124.64.96
Probable Submitter: 86.157.213.12
----
Manual Page -- http://www.php.net/manual/en/language.constants.php
Edit -- https://master.php.net/note/edit/79431
Del: integrated -- https://master.php.net/note/delete/79431/integrated
Del: useless -- https://master.php.net/note/delete/79431/useless
Del: bad code -- https://master.php.net/note/delete/79431/bad+code
Del: spam -- https://master.php.net/note/delete/79431/spam
Del: non-english -- https://master.php.net/note/delete/79431/non-english
Del: in docs -- https://master.php.net/note/delete/79431/in+docs
Del: other reasons-- https://master.php.net/note/delete/79431
Reject -- https://master.php.net/note/reject/79431
Search -- https://master.php.net/manage/user-notes.php