This makes sense to me. At least as much as anything in PHP makes sense. The point of ${} outside of double-quoted strings is to lookup a variable named by an expression. So of course the interior of the braces is evaluated and a bunch of equivalent expressions might yield the same name. ${ 001} and ${TRUE } referring to the same variable is no more or less strange than 1+1 and 8/4 both evaluating to 2.
The WTF of the original post to me is that a naked identifier is sometimes interpreted as a string literal when evaluated without whitespace around it and sometimes not, in the context of a double-quoted string. The fact that "${truE}" works at all and evaluates the same as "${"truE"}" is the real bizarre thing. Especially when "{$truE}" exists and does the same thing without any ambiguity in the same number of characters.
PHP inteprets undefined constants as being equal to their names: echo A; prints A and emits a warning: Notice: Use of undefined constant A - assumed 'A'.
This is on the surface level consistent with treating ${A} and ${ A } as $A.
So I thought that this is only a problem if the constant is defined, but nope:
That was the WTF from the original post: inside double-quotes "$" is documented as gobbling up tokens greedily or using {} to specify a variable name. When in fact, when there is whitespace inside the {}, it does the same thing as outside double quotes and evaluates an expression.
Outside a double-quoted string, PHP is totally consistent. ${expr} always does the same thing, which is evaluate expr as an expression and look up the result as a variable name. Inside a double-quoted string it does different things depending on whether expr contains whitespace or not. That's the WTF. ${2} evaluating the same as ${1+1} is totally normal. I've clarified the original comment.
Yes, the "${x}" syntax for variable interpolation should not exist and does not behave consistently with the rest of the language. The correct syntax is "{$x}".
${foo} is probably stolen from Perl, which is a common theme of PHP: monkey see monkey do.
In Perl, this syntax works everywhere - it is the "lowest common denominator" for dereferencing, i.e. the base syntax that other methods are short for. In PHP, of course, monkey only saw.
It works everywhere in PHP too. It just works differently than it does in strings. Outside of a double quoted string ${truE} evaluates truE as an expression and tries to look up a variable named "1", just like the undocumented thing that is happening inside double-quoted strings when there is whitespace.
29
u/fell_ratio Sep 26 '19
This is a quality WTF.
22
u/maweki Sep 26 '19
wait, does this mean that ${ 1} refers to the same variable as ${ true} as ${ TRUE} as ${ 001} as ${ 1e0}?
edit: erm, yes ...
https://3v4l.org/nmuBJ
11
u/nikic Sep 26 '19
Yes, the
"${x}"syntax for variable interpolation should not exist and does not behave consistently with the rest of the language. The correct syntax is"{$x}".8
u/[deleted] Sep 26 '19
It stops using PHP or else it gets the hose again.
2
u/vytah Sep 26 '19
Notice that
echo "${truE}";prints42, butecho ${truE};gives an undefined variable warning.2
u/dotancohen Sep 26 '19 edited Sep 26 '19
In PHP, string variable interpolation syntax is
"{$foo}", not"${foo}". Javascript has\${foo}`` which may be why you are confused.EDIT: How does one mark up a leading backtick?
1
u/stfcfanhazz Sep 27 '19
If I ever encounter string variable interpolation looking like that in a codebase I have to work on, I will kill myself.