php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #44397 Escape sequences being expanded in single quoted strings
Submitted: 2008-03-10 18:14 UTC Modified: 2008-11-07 15:21 UTC
Votes:3
Avg. Score:4.3 ± 0.9
Reproduced:2 of 3 (66.7%)
Same Version:1 (50.0%)
Same OS:1 (50.0%)
From: johnston dot joshua at gmail dot com Assigned:
Status: Not a bug Package: Documentation problem
PHP Version: 5.2.5 OS: OS X 10.5.2
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: johnston dot joshua at gmail dot com
New email:
PHP Version: OS:

 

 [2008-03-10 18:14 UTC] johnston dot joshua at gmail dot com
Description:
------------
This is either a bug with strings or a documentation bug depending on what you decide is "The PHP Way"

When using backslashes in single quoted strings, a double backslash is being interpreted as the escape sequence for a single backslash. This is contrary to what the php manual leads you to believe.

The php manual for single quoted strings (php.net/types.string) says:

To specify a literal single quote, you will need to escape it with a backslash (\), like in many other languages. If a backslash needs to occur before a single quote or at the end of the string, you need to double it. Note that if you try to escape any other character, the backslash will also be printed! So usually there is no need to escape the backslash itself.

It is my opinion that since escape sequences do not work inside single quoted strings that backslashes should exhibit the same behavior.

e.g
'\'' is a single quote
'\' is a parse error
'\ ' is a backslash followed by a space
'\\' is a single backslash
'\\ ' is two backslashes followed by a space

I understand that this is a difficult issue that a backslash is sometimes an escape sequence and sometimes not. If this cannot be accomplished then the php manual should be updated to reflect this "gotcha".

I did notice a few similar reports regarding this issue and I believe that some clarification should be added.

Reproduce code:
---------------
<?php
echo '\ 1', PHP_EOL;
echo '\\ 2', PHP_EOL;
echo '\\\ 3', PHP_EOL;
echo '\\\\ 4', PHP_EOL;
echo '\\\\\ 5', PHP_EOL;
echo '\\\\\\ 6', PHP_EOL;

Expected result:
----------------
\ 1
\\ 2
\\\ 3
\\\\ 4
\\\\\ 5
\\\\\\ 6

Actual result:
--------------
\ 1
\ 2
\\ 3
\\ 4
\\\ 5
\\\ 6

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-03-10 21:14 UTC] johannes@php.net
Yes, this might be made clearer in the docs. Nice examples :-)
 [2008-11-07 11:07 UTC] vrana@php.net
It's already written in the docs: "If you try to escape any other character, the backslash will also be printed."
 [2008-11-07 14:33 UTC] johnston dot joshua at gmail dot com
My example isn't trying to "escape any other character". My example has a backslash escaped mid-string. The documentation clearly states "If a backslash needs to occur before a single quote or at the end of the string, you need to double it." Is says nothing about backslashes being escaped mid-string.


<?php
echo '\\\ 3', PHP_EOL;
 [2008-11-07 14:58 UTC] johnston dot joshua at gmail dot com
Sorry, I need to follow up again to make this more clear.

Using this example:
echo '\\\ 3', PHP_EOL;

According to the documentation, this should print three backslashes, a space, then the number 3.

The first backslash is not escaping a single quote or a backslash at the end of the string so it should be printed.

The second backslash is not escaping a single quote or a backslash at the end of the string so it should be printed.

The third backslash is "escaping" a space character but that is not a single quote or a backslash at the end of the string so it should be printed.

Then the space, then the 3.

The actual output is \\ 3. That is two backslashes, a space, then a 3. This shows us that the quote you replied with: "If you try to escape any other character, the backslash will also be printed." is completely false.

The documentation is incorrect or the php code for single quoted strings is incorrect. This example clearly contradicts the manual.
 [2008-11-07 15:21 UTC] sean@php.net
Agreed. \\ resolves to \ in a quoted string.
S
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Wed Dec 03 14:00:01 2025 UTC