html源代码中空格的值

发布于 2024-08-22 22:21:52 字数 485 浏览 6 评论 0原文

我正在使用一个函数来生成 php 中的所有输出。使用该函数,我可以控制是否显示这样的代码:

<html><header></header><body><p>Hello World!</p></body></html>

或像这样

<html>
<header>
</header>
<body>
    <p>Hello World!</p>
</body>
</html>

包括缩进和所有内容。

显示缩进和间隔的代码是否有特定的价值(除了看似较慢的加载时间)?我通常不需要查看源代码,因为我可以简单地访问 PHP 文件。在开发过程中,我很可能更喜欢空白,但在生产过程中,它一定是有利的吗?

谢谢!

I'm using a function to generate all output in php. Using that function I can control whether to display the code like this:

<html><header></header><body><p>Hello World!</p></body></html>

or like this

<html>
<header>
</header>
<body>
    <p>Hello World!</p>
</body>
</html>

including the indentation and all.

Is there a particular value to displaying the code indented and spaced (besides seemingly slower loading time)? I usually don't need to view the source code, since I can simply access the PHP file. During development I would most likely prefer whitespace, but when on production would it necessarily be advantageous?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(6

岁月如刀 2024-08-29 22:21:52

如果您可以选择的话,我会将其间隔开,使用空白来使某些内容可读并没有什么问题,而且使用GZip,它使下载差异不再那么重要。你永远不知道什么时候需要调试样式,现在就拥有它会节省你的时间,相信我。

I'd space it out if you have the option, there's nothing wrong with white-space to make something readable, and with GZip it makes the download difference not all that major anyway. You never know when you'll have to debug a style, it'll save you time later by having it pretty now, trust me.

萧瑟寒风 2024-08-29 22:21:52

所有空白都被压缩为一个空格,而不是空无一物,因此存在细微差别。例如:

<img src="image.jpg"><img src="image2.jpg">

会产生与此略有不同的结果:

<img src="image.jpg">
<img src="image2.jpg">

因此,至少在标签之间使用单个空格/换行符。就我个人而言,我更喜欢在实时站点上使用间距,因为它有助于实时调试,并且在使用 gzip 时,空格和无空格之间的差异无论如何都是很小的。

当然,它也会帮助那些可能想看看“它是如何完成的”的新开发者。

All whitespace is condensed to a single space, rather than nothing, so there is a slight difference. For example:

<img src="image.jpg"><img src="image2.jpg">

Will produce slightly different results to this:

<img src="image.jpg">
<img src="image2.jpg">

So at a minimum, use a single space/newline between tags. Personally I prefer using spacing on live sites because it aids live debugging, and when using gzip the difference between space and no-space is tiny anyway.

And of course, it would also help budding new developers who might like to see "how it was done".

暗喜 2024-08-29 22:21:52

我更喜欢省略空格,尤其是在生产中。

您仍然可以通过 Firebug 查看代码。没有理由“查看源代码”。

请注意,空格可能会导致一些问题,因为它们被视为空格。

I prefer to omit whitespaces, especially in production.

You can still view the code through Firebug. there is no reason to do "view source".

Note that spaces can cause some problems, because they are considered as a space.

南城追梦 2024-08-29 22:21:52

如果你删除了空格,有一天当你必须在某个远程客户端计算机上的 Internet Explorer 中查看源代码并且必须费力地浏览一大堆 HTML 标签时,你就会后悔自己的节俭本性。

If you strip out whitespace you'll rue your parsimonious nature one day when you have to View Source in Internet Explorer on some remote client machine and have to wade through a swamp of HTML tags.

乱了心跳 2024-08-29 22:21:52

空白会不必要地积累网络带宽。不,GZIP 不会 100% 修复它。我自己删除了响应中的所有空白,然后通过 GZIP 传递它。唯一关心 HTML 源代码中的空格的是那些对页面源代码的外观感到好奇的 Web 开发人员。它们确实不值得浪费网络带宽——除非您实际上是唯一的访客;)

Whitespace will unnecessarily accumulate network bandwidth. No, GZIP won't fix it up to with 100%. I myself trim all the whitespace from the response and then pass it through GZIP. The only ones who cares about whitespace in HTML source are webdevelopers who are curious how the page source look like. They are really not worth the waste of network bandwidth --unless you're practically the only visitor ;)

才能让你更想念 2024-08-29 22:21:52

您的 Html 是由 Javascript 还是 PHP 生成的?无论哪种方式 - 使用转义字符。

\n = 换行

\t = 制表符

\r = 回车

<?php echo "This is a test. <br> \n"; ?> 

Is Javascript or PHP generating your Html? Either way - utilize an escape character.

\n = new line

\t = tab

\r = carriage return

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