简体   繁体   English

为什么在PHP中print'\\ n'和print'\\\\ n'显示相同的输出?

[英]Why is print '\n' and print '\\n' showing same output in PHP?

<?php
print '\n';
print '\\n';
?>

For the above php code, i got output the following output:( http://ideone.com/vKRtgb ) 对于上面的php代码,我得到了以下输出:( http://ideone.com/vKRtgb

\n\n

So, what is the difference between print'\\n' and print'\\\\n' ? 那么, print'\\n'print'\\\\n'什么区别? I know that singly quoted strings are treated literally, but I expected the output to be \\n\\\\n instead of \\n\\n 我知道单引号字符串按字面意义处理,但我希望输出为\\n\\\\n而不是\\n\\n

Because in single-quoted strings in PHP can only escape single quotes and backslashes. 因为在PHP单引号中的字符串只能转义单引号和反斜杠。 Use double-quoted strings for escaping other characters like \\n . 使用双引号引起来的转义其他字符,例如\\n

From the PHP documentation linked above: 从上面链接的PHP文档中:

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: this means that the other escape sequences you might be used to, such as \\r or \\n, will be output literally as specified rather than having any special meaning . 所有其他反斜杠实例都将被视为文字反斜杠: 这意味着您可能习惯的其他转义序列(例如\\ r或\\ n)将按原样输出,而不具有任何特殊含义

当使用双引号(“)时,PHP仅解释转义字符(转义反斜杠\\和转义单引号\\'除外)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM