为什么要使用换行符 '\n'
我需要一些建议... 我想知道在代码换行符“\n”中使用是否是一个好习惯?目的是什么?
$my_string .= "\n" . "<p>Some values</p>" . "\n";
到目前为止我还没有使用过它,我真的很想知道......你的意见。 谢谢
I need some advice ...
I'd like to know if it is a good practice to use in a code line breaks "\n"? What is the purpose?
$my_string .= "\n" . "<p>Some values</p>" . "\n";
Till now I haven't use it and I'd really like to know ... your opinion.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
您只需要使用“\n”即可使生成的 html 代码更整洁/更易于阅读。没有必要。
You only need to use "\n" to make the resulting html code neater/easier to read. It is not necessary.
新行使生成的代码更易于阅读。您可能认为没有人应该阅读您的代码,但是如果您遇到一些问题,生成的代码也更容易阅读以进行调试。
The new line makes your generated code easier to read. You might think noone should read your code, however if you run into some problems the generated code is easier to read for debugging purposes as well.
\n 也可以在通过套接字进行流传输时使用。有时您需要使用 \r,具体取决于操作系统。
\n can be used when streaming over sockets as well. Sometimes you need to use \r, depending on the operating system.
这是新手程序员很常见的错误。
他们从来不知道 PHP 脚本执行的结果是纯 HTML 代码。
有时程序员必须使用 HTML 来解决问题。
如果代码中没有换行符,这是不可能的。
无论如何,一个好的做法是
,您不需要在 PHP 代码中使用特殊的换行符。
另外,在某些情况下您必须使用换行符。
例如,如果您正在撰写 HTML 电子邮件,则必须添加换行符,否则它们将被强制添加到意想不到的位置。
This is very common mistake of the newbie programmers.
They never have an idea that the result of the PHP script execution is plain HTML code.
And sometimes a programmer have to sort things out with that HTML.
While it's just impossible if there are no linegreaks in the code.
Anyway, a good practice would be
so, you won't need no special linebreaks in the PHP code.
Also, there are some cases where you have to use linebreaks.
For example, if you are composing an HTML email message, you hve to add linebreaks, or they will be forcibly added in the unexpected places.
在写入换行符时,意味着在换行符后显示文本到下一行,
就像在某些文本编辑器中输入一样......
at time of writing to break line, means display text after line-break to next line
like enter in some text editor...
唯一的目的是针对使用浏览器“查看源代码”功能的人——基本上没有人。虽然这被认为是很好的做法,但我几乎从不这样做(因为它毫无意义):)
The only purpose is for people using the "View Source" feature of the browser - basically no one. While it is considered good practice, I almost never do (because it's pointless) :)
对于预标记(预格式化文本):- http://webdesign.about。 com/od/htmltags/f/blfaqpre.htm
所以,你可以这样做
for pre-tag (pre formatted text):- http://webdesign.about.com/od/htmltags/f/blfaqpre.htm
so, you can do this in SO
\n 只是换行的转义符。 PHP 中实际上并不需要转义,但它可以无缝输出新行标记。
\n is just an escape for a new line. An escape is not really required in PHP, but it makes it seamless to output a new line mark.