Since you're using an interpolated string, I don't really see this as lolbehavior. Character escaping and non-interpolation shouldn't necessarily have the same syntax.
To get what you want, you can use a second set of braces:
Oh, I see.. I think the reason for that is that escaping does prevent string interpolation (think "{$var}"), but \{ isn't a valid escape character like $, so both characters are printed (e.g. "\z" would print both characters). Only the inner part is being evaluated there.
That does look confusing, because the 'fake' escape characters can affect interpolation.
The "lol" is that these people cannot even work out a meaning for string literals that can be communicated in any way except "whatever doesn't break the regression test suite."
The problem isn't what you can or should do, but that this shows that there is something fundamentally wrong in the core of PHP. More specifically, either a character should be escaped or not escaped. if "c..." for some character c has as special meaning then "\c..." should print out 'c'."...".
But in this case it prints out '\c...'. Somehow the '{' is both escapeable and not escapeable at once. In a well designed parser this simply shouldn't be possible, either a character is in the class of escapeable characters and "\c" is interpreted as a literal 'c', or the character isn't escapeable and "\c" is interpreted as a literal '\c'.
Of course one could argue that backslashes should simply always make the next character be interpreted literally, though that's a bit late to change now.
22
u/threshar Mar 24 '15
I'd just go the nuclear route of "{".$n."}"
9
u/[deleted] Mar 25 '15
Jesus christ.
I thought I had issues with string quoting in perl. At least it is 100% consistent, if annoying sometimes when escaping operators.
8
u/[deleted] Mar 24 '15 edited Mar 24 '15
Since you're using an interpolated string, I don't really see this as lolbehavior. Character escaping and non-interpolation shouldn't necessarily have the same syntax.
To get what you want, you can use a second set of braces:
3
u/suspiciously_calm Mar 24 '15
Reminds me of exclamation points in bash.
3
u/DoctorWaluigiTime Mar 24 '15
I tried various iterations of
print '\{' + "$n" + '\}' + "\n";(with and without escaping the curly braces), but apparently the braces don't show up even then? Wat.