PHP换行符(\ n)不起作用 [英] PHP Linefeeds (\n) Not Working
问题描述
由于某些原因,在使用PHP输出到文件时,无法使用\n
创建换行符.它只是将"\n
"写入文件.我也尝试过使用"\\n
",它只写"\ n"(符合预期).但是我一生都无法弄清楚为什么在字符串中添加\ n不会创建新行.我也尝试过\r\n
,但是它只是将"\r\n
"附加到文件中的行上.
For some reason I can't use \n
to create a linefeed when outputting to a file with PHP. It just writes "\n
" to the file. I've tried using "\\n
" as well, where it just writes "\n" (as expected). But I can't for the life of me figure out why adding \n to my strings isn't creating new lines. I've also tried \r\n
but it just appends "\r\n
" to the line in the file.
示例:
error_log('test\n', 3, 'error.log');
error_log('test2\n', 3, 'error.log');
输出:
test\ntest2\n
在重要的情况下在OSX上使用MAMP(也许是某种PHP配置的事情?).
Using MAMP on OSX in case that matters (some sort of PHP config thing maybe?).
有什么建议吗?
推荐答案
使用双引号. "test\n"
可以正常工作(或者使用'test' . PHP_EOL
).
Use double quotes. "test\n"
will work just fine (Or, use 'test' . PHP_EOL
).
如果字符串用双引号()引起来,PHP将为特殊字符解释更多的转义序列:
If the string is enclosed in double-quotes ("), PHP will interpret more escape sequences for special characters:
http://php.net/manual/en/language.types .string.php
这篇关于PHP换行符(\ n)不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!