为什么要使用换行符 '\n'

发布于 2024-12-18 01:04:06 字数 182 浏览 3 评论 0原文

我需要一些建议... 我想知道在代码换行符“\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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

过期以后 2024-12-25 01:04:06

您只需要使用“\n”即可使生成的 html 代码更整洁/更易于阅读。没有必要。

You only need to use "\n" to make the resulting html code neater/easier to read. It is not necessary.

不必你懂 2024-12-25 01:04:06

新行使生成的代码更易于阅读。您可能认为没有人应该阅读您的代码,但是如果您遇到一些问题,生成的代码也更容易阅读以进行调试。

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.

居里长安 2024-12-25 01:04:06

\n 也可以在通过套接字进行流传输时使用。有时您需要使用 \r,具体取决于操作系统。

\n can be used when streaming over sockets as well. Sometimes you need to use \r, depending on the operating system.

¢好甜 2024-12-25 01:04:06

这是新手程序员很常见的错误。
他们从来不知道 PHP 脚本执行的结果是纯 HTML 代码。
有时程序员必须使用 HTML 来解决问题。
如果代码中没有换行符,这是不可能的。

无论如何,一个好的做法是

$my_string = "Some values";
?>
<p><?=$my_string</p>

,您不需要在 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

$my_string = "Some values";
?>
<p><?=$my_string</p>

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.

临风闻羌笛 2024-12-25 01:04:06

在写入换行符时,意味着在换行符后显示文本到下一行,

就像在某些文本编辑器中输入一样......

at time of writing to break line, means display text after line-break to next line

like enter in some text editor...

許願樹丅啲祈禱 2024-12-25 01:04:06

唯一的目的是针对使用浏览器“查看源代码”功能的人——基本上没有人。虽然这被认为是很好的做法,但我几乎从不这样做(因为它毫无意义):)

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) :)

筱武穆 2024-12-25 01:04:06

对于预标记(预格式化文本):- http://webdesign.about。 com/od/htmltags/f/blfaqpre.htm

所以,你可以这样做

1
2
3
4
5
6
7
8

for pre-tag (pre formatted text):- http://webdesign.about.com/od/htmltags/f/blfaqpre.htm

so, you can do this in SO

1
2
3
4
5
6
7
8
夜清冷一曲。 2024-12-25 01:04:06

\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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文