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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With