#44397 [Bgs]: Escape sequences being expanded in single quoted strings

From: Date: Fri, 07 Nov 2008 15:21:27 +0000
Subject: #44397 [Bgs]: Escape sequences being expanded in single quoted strings
References: 1  Groups: php.doc.bugs 
Request: Send a blank email to doc-bugs+get-1462@lists.php.net to get a copy of this message
ID: 44397 Updated by: sean@php.net Reported By: johnston dot joshua at gmail dot com Status: Bogus Bug Type: Documentation problem Operating System: OS X 10.5.2 PHP Version: 5.2.5 New Comment: Agreed. \\ resolves to \ in a quoted string. S Previous Comments: ------------------------------------------------------------------------ [2008-11-07 14:58:07] 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 14:33:25] 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 11:07:28] 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-03-10 21:14:02] johannes@php.net Yes, this might be made clearer in the docs. Nice examples :-) ------------------------------------------------------------------------ [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

« previous php.doc.bugs (#1462) next »