From:             swift at alum dot mit dot edu
Operating system: Irrelevant
PHP version:      Irrelevant
PHP Bug Type:     Documentation problem
Bug description:  self-contradictory documentation of escaping in single-quoted 
strings

Description:
------------
I quote from the node on strings in the PHP manual
(www.php.net/manual/en/language.types.string.php) where it discusses
single-quoted strings:

I break into sentences which I reference below:

(1) To specify a literal single quote, you will need to escape it with a
backslash (\), like in many other languages.

(2) If a backslash needs to occur before a single quote or at the end of
the string, you need to double it.

(3) Note that if you try to escape any other character, the backslash will
also be printed!

(4) So usually there is no need to escape the backslash itself.

Sentence (1) is clear, though "will" should be omitted for better
clarity.

Sentence (2) means that a literal backslash needs to be escaped in only
two cases: when it appears before a literal single quote and when it is
the last character of the string.  This is of course incorrect (it is
contradicted by a correct example further down the page whose output is
'You deleted C:\*.*?').  What the author intends to say is much better
expressed by the following:  "An escaped backslash (\\) likewise specifies
a literal backslash, but it is not normally necessary to escape a literal
backslash.  Escaping a backslash is necessary only when the context would
otherwise make the backslash into an escape operator -- that is when it
appears immediately before another backslash or the single quote
terminating the string."

Sentence (3) is unclear because it is unclear whether the reference of
"other" is "backslash" or "single quote" or both.  Also "try to escape" is
a confusing hypothetical.  A good revision would be:  "A backslash followed
by any character besides a single quote or another backslash is a literal
backslash."

Sentence (4) is confusing because it's not clear that "the backslash
itself" refers to a literal backslash or an escape-operator backslash. 
Omit this sentence: this section should deal only with how to specify a
single quoted string.      Discussion of applications of strings (such as
regexps) which also use the backslash as an escape operator belongs
somewhere else -- or at least should be clearly separate from the
discussion of simply how to specify the contents of a string.

"Stringing" together the suggested revisions above:

To specify a literal single quote, you need to escape it with a backslash
(\), like in many other languages.  An escaped backslash (\\) likewise
specifies a literal backslash, but it is not normally necessary to escape
a literal backslash.  Escaping a backslash is necessary only when the
context would otherwise make the backslash into an escape operator -- that
is when it appears immediately before another backslash or the single quote
terminating the string.  A backslash followed by any character besides a
single quote or another backslash is a literal backslash.



-- 
Edit bug report at http://bugs.php.net/?id=36710&edit=1
-- 
Try a CVS snapshot (PHP 4.4): 
http://bugs.php.net/fix.php?id=36710&r=trysnapshot44
Try a CVS snapshot (PHP 5.1): 
http://bugs.php.net/fix.php?id=36710&r=trysnapshot51
Try a CVS snapshot (PHP 6.0): 
http://bugs.php.net/fix.php?id=36710&r=trysnapshot60
Fixed in CVS:                 http://bugs.php.net/fix.php?id=36710&r=fixedcvs
Fixed in release:             
http://bugs.php.net/fix.php?id=36710&r=alreadyfixed
Need backtrace:               http://bugs.php.net/fix.php?id=36710&r=needtrace
Need Reproduce Script:        http://bugs.php.net/fix.php?id=36710&r=needscript
Try newer version:            http://bugs.php.net/fix.php?id=36710&r=oldversion
Not developer issue:          http://bugs.php.net/fix.php?id=36710&r=support
Expected behavior:            http://bugs.php.net/fix.php?id=36710&r=notwrong
Not enough info:              
http://bugs.php.net/fix.php?id=36710&r=notenoughinfo
Submitted twice:              
http://bugs.php.net/fix.php?id=36710&r=submittedtwice
register_globals:             http://bugs.php.net/fix.php?id=36710&r=globals
PHP 3 support discontinued:   http://bugs.php.net/fix.php?id=36710&r=php3
Daylight Savings:             http://bugs.php.net/fix.php?id=36710&r=dst
IIS Stability:                http://bugs.php.net/fix.php?id=36710&r=isapi
Install GNU Sed:              http://bugs.php.net/fix.php?id=36710&r=gnused
Floating point limitations:   http://bugs.php.net/fix.php?id=36710&r=float
No Zend Extensions:           http://bugs.php.net/fix.php?id=36710&r=nozend
MySQL Configuration Error:    http://bugs.php.net/fix.php?id=36710&r=mysqlcfg

Reply via email to