Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is '\n' === '\\n' true in php?

I understand that:

'\n' // literally the backslash character followed by the character for lowercase n
"\n" // interpreted by php as the newline character

But for the life of me, I can't understand why '\n' === '\\n'. In my mind, '\\n' would equal three separate characters: two separate backslashes, followed by the letter n.

Why is '\n' === '\\n' true in PHP?

like image 979
JoeCortopassi Avatar asked Jul 02 '12 23:07

JoeCortopassi


1 Answers

From the manual (section on single quoted strings):

To specify a literal single quote, escape it with a backslash (\). To specify a literal backslash, double it (\\). All other instances of backslash will be treated as a literal backslash

so in a single quoted string \n is two characters, but \\n is a literal backslash followed by the letter 'n' - i.e. the same two characters.

like image 144
Tim Fountain Avatar answered Oct 30 '22 09:10

Tim Fountain