PHP-如何创建换行符? [英] PHP - how to create a newline character?
问题描述
在PHP中,我试图创建换行符:
In PHP I am trying to create a newline character:
echo $clientid;
echo ' ';
echo $lastname;
echo ' ';
echo '\r\n';
然后,我在记事本中打开创建的文件,它按字面意义写换行符:
Afterwards I open the created file in Notepad and it writes the newline literally:
1个John Doe \ r \ n 1个John Doe \ r \ n 1个John Doe \ r \ n
1 John Doe\r\n 1 John Doe\r\n 1 John Doe\r\n
我尝试了\r\n
的许多变体,但没有任何效果.为什么换行符不变成换行符?
I have tried many variations of the \r\n
, but none work. Why isn't the newline turning into a newline?
推荐答案
Only double quoted strings interpret the escape sequences \r
and \n
as '0x0D' and '0x0A' respectively, so you want:
"\r\n"
单引号字符串,另一方面,只知道转义序列\\
和\'
.
Single quoted strings, on the other hand, only know the escape sequences \\
and \'
.
因此,除非您将单引号字符串与在其他位置生成的换行符连接起来(例如,使用双引号字符串"\r\n"
或使用 chr
函数 chr(0x0D).chr(0x0A)
),在单个带引号的字符串中有换行符的唯一另一种方法是使用编辑器按字面值键入它:
So unless you concatenate the single quoted string with a line break generated elsewhere (e. g., using double quoted string "\r\n"
or using chr
function chr(0x0D).chr(0x0A)
), the only other way to have a line break within a single quoted string is to literally type it with your editor:
$s = 'some text before the line break
some text after';
如果需要某些特定的字符序列(例如,\r\n
),请确保检查编辑器的换行设置.
Make sure to check your editor for its line break settings if you require some specific character sequence (\r\n
for example).
这篇关于PHP-如何创建换行符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!