5

Can anyone tell me what is happening here?

<?php
// true
var_dump('\\ ' === '\ ');

// false
var_dump('\\\\ ' === '\\ ');

// true
var_dump('\\\\ ' === '\\\ ');
3
  • 2
    Have you tried printing the strings? Commented Mar 28, 2012 at 12:58
  • 1
    Yeah, dumped the hexcodes. They're really different, it seems that a \\ gets collapsed to \ for some reason, but it's in a single-quoted string, it shouldn't happen there. Commented Mar 28, 2012 at 13:01
  • 2
    There's no difference whether a string is single or double quoted. The escape character works identically in both cases. Commented Mar 28, 2012 at 13:02

3 Answers 3

8

\ inside a string literal introduces several types of escape sequences, \\ is the escape sequence for a literal "\". But, \s that don't resolve to an escape sequence are also taken as literal "\".

Therefor, '\\ ' stands for the string "\ ", '\\\\ ' stands for the string "\\ ", just as '\\\ '. Try:

echo '\\\\ ';   -> \\ 

See http://php.net/manual/en/language.types.string.php#language.types.string.syntax.single.

Sign up to request clarification or add additional context in comments.

3 Comments

I had no idea that \\ works in a single-quoted string, thought it only works on ' itself.
@DaliborKarlović It is set up like that so you can have a single quoted string that ends with a backslash, if you couldn't escape the backslash you wouldn't be able to wrap string\ in single quotes.
Yeah, I get it, my bad is that I was trying to use it inside a regex searching for a literal \n (not a control char), so preg_quote('#\n#') produced a string which matched, but var_dump()-ing the value produced by preg_quote() and using that instead did not. Now I had to use #(\\\n){2,}#
4

In single quoted strings, no escape sequences are interpolated. A backslash is only an escape character if it immediately precedes a single quote, or a backslash.

So:

var_dump('\\ '); // String (2) "\ "
var_dump('\ '); // String (2) "\ "
// They do match

var_dump('\\\\ '); // String (3) "\\ "
var_dump('\\ '); // String (2) "\ "
// They don't match

var_dump('\\\\ '); // String (3) "\\ "
var_dump('\\\ '); // String (3) "\\ "
// They do match

This is expected and documented behaviour, although it can be difficult to wrap you head around on the face of it.

Comments

1

In 1st example you're comparing

"\ " and "\ " which is TRUE

in 2nd

"\\ " and "\ " which is FALSE

in 3rd

"\\ " and "\\ " which is TRUE

If you print out your strings

$s = array('\ ', '\\ ', '\\\ ', '\\\\ ');
var_dump($s);

you'll get

array(4) {
  [0]=>
  string(2) "\ "
  [1]=>
  string(2) "\ "
  [2]=>
  string(3) "\\ "
  [3]=>
  string(3) "\\ "
}

All double-slashes '\\' have been converted into single-slashes '\' and sigle-slashes remain the same. Escaping works the same way inside single and double-quoted strings.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.