如果我将一堆代码放在一个 php 标签中与将其分解,会有什么不同吗?

发布于 2024-11-18 10:40:25 字数 505 浏览 2 评论 0原文

除了个人喜好之外,还有什么区别吗?

<?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 技术交流群。

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

发布评论

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

评论(4

雪落纷纷 2024-11-25 10:40:25

第一个版本将在生成的输出中输出一个额外的换行符,因为 ?>之间有一个换行符:

?>
<?php

这是唯一的区别;两者之间没有任何明显的性能影响。

The first version will output an extra newline character into the generated output, since there's one between the ?> and the <?php:

?>
<?php

That is the only difference; there isn't any noticeable performance impact between the two.

演出会有结束 2024-11-25 10:40:25

之外的所有内容都被视为输出。这意味着,

?>
<?php

可能输出一些东西。 “May”,因为 ?> 后面的换行符是标记的一部分,因此不会返回。但是像这样的东西

?>
  <?php

有两个空格echoed。问题是,在某些内容返回到浏览器后,您无法再设置任何标头。

Everything outside <?php ?> is treated as output. This means, that

?>
<?php

may output something. "May" because the newline after ?> is part of the tag and therefore not returned. But with something like

?>
  <?php

there are two whitespace echoed. The problem is, that you cannot set any headers anymore, after something is returned to the browser.

非要怀念 2024-11-25 10:40:25

正如 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.

烛影斜 2024-11-25 10:40:25

是的,这会有所不同。看一下:

<?php
$uselessvar = 1;
?>
<?php
header('Location: /'); // This will not work
?>

<?php
$uselessvar = 1;
header('Location: /'); // This will work
?>

在第一个示例中,第一个结束标记 ?> 和第二个开始标记 之间有一个新行。该新行被视为输出并将其发送到客户端。如果在调用之前将任何输出发送到客户端,header 函数将无法工作。这就是为什么第一个例子不起作用而第二个例子却起作用。

更一般地说,最好仅在需要时使用结束标记 ?> 以避免此类错误。
例如,您不必在 php 文件末尾放置结束标记 ?>。有时我们会看到一个文件由一个结束标签终止,然后是一个新的空行。这个新的空行具有与上面相同的效果,并且可以崩溃/更改任何脚本。

Yes it makes a difference. Look at this:

<?php
$uselessvar = 1;
?>
<?php
header('Location: /'); // This will not work
?>

<?php
$uselessvar = 1;
header('Location: /'); // This will work
?>

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.

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