如果我将一堆代码放在一个 php 标签中与将其分解,会有什么不同吗?
除了个人喜好之外,还有什么区别吗?
<?php
$meta = get_post_meta(get_the_ID(), 'rw_strNum', true);
'' != $meta and print "$meta";
?>
<?php
$meta = get_post_meta(get_the_ID(), 'rw_strName', true);
'' != $meta and print "$meta";
?>
与此相反
<?php
$meta = get_post_meta(get_the_ID(), 'rw_strNum', true);
'' != $meta and print "$meta";
$meta = get_post_meta(get_the_ID(), 'rw_strName', true);
'' != $meta and print "$meta";
?>
Besides personal preference, does it make any difference?
<?php
$meta = get_post_meta(get_the_ID(), 'rw_strNum', true);
'' != $meta and print "$meta";
?>
<?php
$meta = get_post_meta(get_the_ID(), 'rw_strName', true);
'' != $meta and print "$meta";
?>
as opposed to this
<?php
$meta = get_post_meta(get_the_ID(), 'rw_strNum', true);
'' != $meta and print "$meta";
$meta = get_post_meta(get_the_ID(), 'rw_strName', true);
'' != $meta and print "$meta";
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
第一个版本将在生成的输出中输出一个额外的换行符,因为
?>
和之间有一个换行符:
这是唯一的区别;两者之间没有任何明显的性能影响。
The first version will output an extra newline character into the generated output, since there's one between the
?>
and the<?php
:That is the only difference; there isn't any noticeable performance impact between the two.
之外的所有内容都被视为输出。这意味着,
可能输出一些东西。 “May”,因为
?>
后面的换行符是标记的一部分,因此不会返回。但是像这样的东西有两个空格
echo
ed。问题是,在某些内容返回到浏览器后,您无法再设置任何标头。Everything outside
<?php ?>
is treated as output. This means, thatmay output something. "May" because the newline after
?>
is part of the tag and therefore not returned. But with something likethere are two whitespace
echo
ed. The problem is, that you cannot set any headers anymore, after something is returned to the browser.正如 knittl 所说,它不会产生任何明显的差异。唯一的区别是,当您将 PHP 和 HTML 混合在一起时(例如在制作模板时),它将使您的 HTML 更易于阅读。
无论如何,php 解析器都会遍历整个文件,我怀疑遇到结束标记,然后开始标记对解析/解析速度有任何影响。
Like knittl said, it will not make any visible difference. The only difference is it will make your HTML easier to read when you mix PHP and HTML together, for example when doing templates.
The php parser goes through the whole file regardless and I doubt encountering a closing tag and then an opening tag has any impact on parsing/speed of parsing.
是的,这会有所不同。看一下:
在第一个示例中,第一个结束标记
?>
和第二个开始标记之间有一个新行。该新行被视为输出并将其发送到客户端。如果在调用之前将任何输出发送到客户端,
header
函数将无法工作。这就是为什么第一个例子不起作用而第二个例子却起作用。更一般地说,最好仅在需要时使用结束标记
?>
以避免此类错误。例如,您不必在 php 文件末尾放置结束标记
?>
。有时我们会看到一个文件由一个结束标签终止,然后是一个新的空行。这个新的空行具有与上面相同的效果,并且可以崩溃/更改任何脚本。Yes it makes a difference. Look at this:
In the first example, there is a new line between the first closing tag
?>
and the second opening tag<?php
. This new line is treated as an output and it's send to the client.header
function could not work if any output is sent to the client before its called. That's why the first example will not work where the second will.In a more general way, it's better to use the closing tag
?>
only where you need it, to avoid such errors.For example, you don't have to put a closing tag
?>
at the end of a php file. Sometimes we see a file terminated by a closing tag then a new empty line. This new empty line has the same effect as above, and can crash/alter any script.