为什么'\ n'==='\\ n'在php中为true? [英] Why is '\n' === '\\n' true in php?
本文介绍了为什么'\ n'==='\\ n'在php中为true?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我了解:
'\n' // literally the backslash character followed by the character for lowercase n
"\n" // interpreted by php as the newline character
但是对于我的一生,我不明白为什么'\n' === '\\n'
.在我看来,'\\n'
等于三个单独的字符:两个单独的反斜杠,后跟字母n.
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.
为什么'\n' === '\\n'
在PHP中是true?
Why is '\n' === '\\n'
true in PHP?
推荐答案
反斜杠仍然是单引号字符串中的转义字符(它对文字单引号进行转义).
The backslash is still an escape character in single-quoted strings (it escapes literal single quotes).
例如,这是非法的(因为反斜杠转义了右引号):
This is illegal for instance (since the backslash escapes the closing quote):
$path = 'C:\';
因此\\
必须映射到文字反斜杠,以避免无意的转义.
So \\
must map to a literal backslash to avoid inadvertent escaping.
这篇关于为什么'\ n'==='\\ n'在php中为true?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文