|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2021-11-18 11:06 UTC] shealavington at gmail dot com
Description:
------------
When curly braces are escaped within a string "", the back-slash isn't removed \{. For example `"hello\{$foo}"` is outputting `hello\{bar}` instead of `hello{bar}`
Test script:
---------------
<?php
$foo = "bar";
$test_script = "hello\{$foo}"
//- Explanation output below.
echo "If `\` is the escape character, why are the escaped characters and the escape character both shown?<br>";
echo "Res: <b>c:\path\hello\{$foo}</b><br>";
echo "Expected: <b>c:\path\hello"."{"."$foo}</b><br>";
echo "<br>Notice how in this example, I haven't used a `\` but the `{}` characters are gone when not escaped?<br>";
echo "Res: <b>c:\path\hello{$foo}</b><br>";
echo "<br>If I use `\ n`, it replaces the `\`, I don't see `\ n` like I do in the first example above.<br>";
echo "Res: <b>c:\path\hello\note\{$foo}</b><br>";
Expected result:
----------------
`hello{bar}`
Actual result:
--------------
`hello\{bar}`
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Tue Nov 18 18:00:01 2025 UTC |
The docs do not list \$ as escape sequence[1]. They are actually very explicit about that[2]: | Since { can not be escaped, […] What you're looking for is <https://3v4l.org/hndfP>. [1] <https://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.double> [2] <https://www.php.net/manual/en/language.types.string.php#language.types.string.parsing.complex>I now see that `\{` is not an escape sequence. However, that doesn't explain why the following ``` $foo = "bar"; var_dump("hello\{$foo}"); ``` outputs `"hello\{bar}"` instead of `"hello\bar"`... The curly braces should act as a variable wrap and should therefore be removed? Please do correct me if I'm mis-understanding here.Notice how with this example I actually get one output with curlys and one without, which would suggest to me that there is a bug in the template parsing. ``` $foo = "bar"; var_dump("hello\{$foo}"); var_dump("hello\x{$foo}x"); ```Yeah, this is actually unclear. "hello\{$foo}" This looks like complex string interpolation syntax, but is apparently parsed as simple string interpolation syntax, so $foo is being replaced with its value. So, I assume, that the backslash acts as escape for the open *and* the closing curly braces.Indeed unclear, and most confusing. I'm working with a filesystem path within a larger string. I need to insert my variable after a backslash. For simplicity, we're using curly braces around all of our variables used within the string, and this situation doesn't allow us to do so. ``` $foo = 'bar'; $newFoo = "(FILENAME='C:\folder\{$var}.txt')" ``` outputs a string with a now invalid file path due to the parsing inconsistency. ``` (FILENAME='C:\folder\{bar}.txt') ``` As you mentioned, `\{` should not be escaping, but does. Hopefully this issue can be looked into being fixed for later versions.Thanks for the further clarification! > $newFoo = "(FILENAME='C:\folder\{$var}.txt')" You really should escape the backslashes in the first place; then you get the desired output: <https://3v4l.org/EKFq2>. > As you mentioned, `\{` should not be escaping, but does. Indeed: <https://3v4l.org/lLZqF>. That needs to be fixed in the docs.Nah, there is nothing special about \{[1]: | As in single quoted strings, escaping any other character will | result in the backslash being printed too. Still, it makes sense to add an example, since backslashes in filenames and the given use-case are common on Windows. [1] <https://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.double>