回显一行输出或关闭php标签和写出html代码有什么区别吗?

时间:2021-10-31 03:54:01

I'm almost sure the answer to this is "None at all!" but I'll ask anyway. If you have a conditional statement in PHP which echos a line of html, is there any difference performance-wise between these 2 examples:

我几乎可以肯定答案是“根本没有!”但无论如何我会问。如果你在PHP中有一个条件语句,它回应了一行html,那么这两个例子之间在性能方面有什么不同:

<?php if ($output) { ?>
  <h2><?=$output;?></h2>
<?php } ?>

and

<?php if ($output) { echo "<h2>".$output."</h2>"; } ?>

10 个解决方案

#1


The answer literally is "None at all". Consider

字面上的答案是“根本没有”。考虑

<span>
<?php
  echo $myMessage;
?>
</span>

and

<?php
  echo "<span>\n";
  echo $myMessage;
  echo "</span>";
?>

I'm going from memory (of a few years ago), but at that point the Zend bytecode compiler produces essentially identical output; "literal" HTML was compiled into an echo statement containing the text.

我将从内存(几年前)开始,但在那时,Zend字节码编译器产生基本相同的输出; “literal”HTML被编译成包含文本的echo语句。

#2


I don't think there is a noticeable performance difference. I use whatever variant makes my code more readable.

我认为没有明显的性能差异。我使用任何变体使我的代码更具可读性。

#3


The <?= ?> tags are invalid, unless you have short_open_tags enabled, which is not recommended.

标记无效,除非您启用了short_open_tags,不建议这样做。

#4


If you are really into micro optimization, you should start with changing

如果你真的进入微优化,你应该从改变开始

echo "<h2>".$output."</h2>";

into:

echo '<h2>', $output, '</h2>';

(no variable expansion and no string concatenation)

(没有变量扩展,也没有字符串连接)

#5


Why do so many PHP developers try to optimize such things? It's the same as the debate about single- and double-quoted strings. If these performance differences matter in any way, you should really be using another language.

为什么这么多PHP开发人员试图优化这些东西呢?这与关于单引号和双引号字符串的争论是一样的。如果这些性能差异有任何影响,那么您应该使用其他语言。

#6


This is called micro optimization, you can't rely on such performance differences

这称为微优化,你不能依赖这种性能差异

But this is better on performance in by few nano seconds

但这在几纳秒的性能上更好

<?php if ($output) { ?>
  <h2><?=$output;?></h2>
<?php } ?>

#7


The performance would be negligable and not worth worrying about in practice. The first option would be the preferred method as it's more readable and uses php as it was meant to be used (templating).

表现可以忽略不计,在实践中不值得担心。第一个选项是首选方法,因为它更具可读性,并且使用了PHP,因为它本意使用(模板化)。

Theoretically though, I'm guessing the first method would be faster as the language has less to parse. The straight html text is just returned without processing.

从理论上讲,我猜第一种方法会更快,因为语言解析得更少。直接的html文本只是在没有处理的情况下返回。

#8


I prefer option 1 as it makes it easier to edit the HTML in most cases - specially when dealing with divs with inner content.

我更喜欢选项1,因为它在大多数情况下更容易编辑HTML - 特别是在处理具有内部内容的div时。

Performance is negligible in both cases.

两种情况下的表现都可以忽略不计。

#9


Why not just test it out ? by sniffing or taking a look at the source for a page in your favourite browser.

为什么不试试呢?通过嗅探或查看您喜欢的浏览器中页面的来源。

#10


I had to know so ran a few different blocks in a loop of 10000 iterations. Lesson learned: Don't bother. Write the code to be readable and maintainable. Use the MVC pattern... :)

我不得不知道在10000次迭代循环中运行了几个不同的块。获得的经验:不要打扰。编写可读和可维护的代码。使用MVC模式...... :)

time: 2.66 microseconds

时间:2.66微秒

<? if ($output) { ?>
    <h2><?=$output;?></h2>
<? } ?>

time: 1.65 microseconds

时间:1.65微秒

<? if ($output) { echo "<h2>$output</h2>"; } ?>

time: 1.65 microseconds

时间:1.65微秒

<? if ($output) { echo "<h2>".$output."</h2>"; } ?>

time: 1.64 microseconds

时间:1.64微秒

<? if ($output) { echo '<h2>'.$output.'</h2>'; } ?>

#1


The answer literally is "None at all". Consider

字面上的答案是“根本没有”。考虑

<span>
<?php
  echo $myMessage;
?>
</span>

and

<?php
  echo "<span>\n";
  echo $myMessage;
  echo "</span>";
?>

I'm going from memory (of a few years ago), but at that point the Zend bytecode compiler produces essentially identical output; "literal" HTML was compiled into an echo statement containing the text.

我将从内存(几年前)开始,但在那时,Zend字节码编译器产生基本相同的输出; “literal”HTML被编译成包含文本的echo语句。

#2


I don't think there is a noticeable performance difference. I use whatever variant makes my code more readable.

我认为没有明显的性能差异。我使用任何变体使我的代码更具可读性。

#3


The <?= ?> tags are invalid, unless you have short_open_tags enabled, which is not recommended.

标记无效,除非您启用了short_open_tags,不建议这样做。

#4


If you are really into micro optimization, you should start with changing

如果你真的进入微优化,你应该从改变开始

echo "<h2>".$output."</h2>";

into:

echo '<h2>', $output, '</h2>';

(no variable expansion and no string concatenation)

(没有变量扩展,也没有字符串连接)

#5


Why do so many PHP developers try to optimize such things? It's the same as the debate about single- and double-quoted strings. If these performance differences matter in any way, you should really be using another language.

为什么这么多PHP开发人员试图优化这些东西呢?这与关于单引号和双引号字符串的争论是一样的。如果这些性能差异有任何影响,那么您应该使用其他语言。

#6


This is called micro optimization, you can't rely on such performance differences

这称为微优化,你不能依赖这种性能差异

But this is better on performance in by few nano seconds

但这在几纳秒的性能上更好

<?php if ($output) { ?>
  <h2><?=$output;?></h2>
<?php } ?>

#7


The performance would be negligable and not worth worrying about in practice. The first option would be the preferred method as it's more readable and uses php as it was meant to be used (templating).

表现可以忽略不计,在实践中不值得担心。第一个选项是首选方法,因为它更具可读性,并且使用了PHP,因为它本意使用(模板化)。

Theoretically though, I'm guessing the first method would be faster as the language has less to parse. The straight html text is just returned without processing.

从理论上讲,我猜第一种方法会更快,因为语言解析得更少。直接的html文本只是在没有处理的情况下返回。

#8


I prefer option 1 as it makes it easier to edit the HTML in most cases - specially when dealing with divs with inner content.

我更喜欢选项1,因为它在大多数情况下更容易编辑HTML - 特别是在处理具有内部内容的div时。

Performance is negligible in both cases.

两种情况下的表现都可以忽略不计。

#9


Why not just test it out ? by sniffing or taking a look at the source for a page in your favourite browser.

为什么不试试呢?通过嗅探或查看您喜欢的浏览器中页面的来源。

#10


I had to know so ran a few different blocks in a loop of 10000 iterations. Lesson learned: Don't bother. Write the code to be readable and maintainable. Use the MVC pattern... :)

我不得不知道在10000次迭代循环中运行了几个不同的块。获得的经验:不要打扰。编写可读和可维护的代码。使用MVC模式...... :)

time: 2.66 microseconds

时间:2.66微秒

<? if ($output) { ?>
    <h2><?=$output;?></h2>
<? } ?>

time: 1.65 microseconds

时间:1.65微秒

<? if ($output) { echo "<h2>$output</h2>"; } ?>

time: 1.65 microseconds

时间:1.65微秒

<? if ($output) { echo "<h2>".$output."</h2>"; } ?>

time: 1.64 microseconds

时间:1.64微秒

<? if ($output) { echo '<h2>'.$output.'</h2>'; } ?>