Re: Constants in strings
| From: | Dave Wilson | Date: | Wed, 06 Jul 2011 14:11:07 +0000 |
| Subject: | Re: Constants in strings | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-313950@lists.php.net to get a copy of this message | ||
On Wed, 06 Jul 2011 12:56:21 +0100, Stuart Dallas wrote:
> My guess is that the preceding $ causes PHP to interpret the next token
> "{XYZ}" as a variable or a constant, but without that preceding $ it has
> no way to know you're trying to use a constant. As Curtis points out,
> the only way to insert a constant into a string is through
> concatenation.
>
> -Stuart
OK. I should have made myself clearer - I was making an observation with
regards to constant parsing in strings rather than looking for advice. My
bad.
My third example showed that "{${XYZ}}" would echo the value of the
variable called the value of XYZ:
<?php
define ('XYZ','ABC');
$ABC="huh!";
echo "{${XYZ}}\n";
?>
Output - huh!
We could easily re-write the 'echo' line above to be:
echo "{${constant('XYZ'}}\n";
But my example shows that PHP *is* accessing the value of a constant
without any jiggery-pokery or hacks (e.g. http://www.php.net/manual/en/
language.types.string.php#91628) as it is retrieving the value of ABC
from the XYZ constant and then looking for a variable of that name.
I admit that I'm no C coder but it may be possible (note, the word "may")
that a change of code within the PHP source tree will allow us to use
something like echo "{{XYZ}}" to access the constant value.
Cheers
Dave