#44397 [Opn]: Escape sequences being expanded in single quoted strings
| From: | johannes@php.net | Date: | Mon, 10 Mar 2008 21:14:02 +0000 |
| Subject: | #44397 [Opn]: Escape sequences being expanded in single quoted strings | ||
| References: | 1 | Groups: | php.doc.bugs |
| Request: | Send a blank email to doc-bugs+get-592@lists.php.net to get a copy of this message | ||
ID: 44397
Updated by: johannes@php.net
Reported By: johnston dot joshua at gmail dot com
Status: Open
-Bug Type: Strings related
+Bug Type: Documentation problem
Operating System: OS X 10.5.2
PHP Version: 5.2.5
New Comment:
Yes, this might be made clearer in the docs. Nice examples :-)
Previous Comments:
------------------------------------------------------------------------
[2008-03-10 18:14:15] 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
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=44397&edit=1