哪一个更快?HTML单独或嵌入PHP?

uto*_*pia -1 php

我有两页:

index.php文件:

<?php
    $mtime = microtime();
    $mtime = explode(" ",$mtime);
    $mtime = $mtime[1] + $mtime[0];
    $starttime = $mtime;
    $text = '
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    </head>
      <body>
        html content
      </body>
    </html>';
    echo $text;
    $mtime = microtime();
    $mtime = explode(" ",$mtime);
    $mtime = $mtime[1] + $mtime[0];
    $endtime = $mtime;
    $totaltime = ($endtime - $starttime);
    echo "This page was created in ".$totaltime." seconds";
?>
Run Code Online (Sandbox Code Playgroud)

和index2.php:

<?php
   $mtime = microtime();
   $mtime = explode(" ",$mtime);
   $mtime = $mtime[1] + $mtime[0];
   $starttime = $mtime;
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<?php echo 'html content'; ?>
</body>
</html> 
<?php
   $mtime = microtime();
   $mtime = explode(" ",$mtime);
   $mtime = $mtime[1] + $mtime[0];
   $endtime = $mtime;
   $totaltime = ($endtime - $starttime);
   echo "This page was created in ".$totaltime." seconds";
?>
Run Code Online (Sandbox Code Playgroud)

index.php的测试结果:

html content This page was created in 6.1988830566406E-5 seconds
Run Code Online (Sandbox Code Playgroud)

对于index2.php:

html contentThis page was created in 6.4849853515625E-5 seconds
Run Code Online (Sandbox Code Playgroud)

为什么HTML嵌入PHP比index2.php更快?

Joh*_*ker 10

这不是一个真正的答案,但我不禁要问你为什么认为这很重要?(如果你担心将事情优化到这个级别,那么我怀疑PHP可能不是理想的语言.)

从本质上讲,代码易读性,一致性以及任何有助于长期维护的方法都比这种微优化更重要,如果您遇到性能问题,我很乐意将大量资金放在它赢得的事实上无论如何输出HTML /是否弹出PHP代码都会阻塞很多东西.

因此,如果您遇到性能问题,请分析代码以找出问题所在 - 不要浪费时间担心什么是"最佳" - 在这种情况下,"最佳"解决方案对于程序员来说是最简单的(s) )从事该项目.

  • 这是完美的,唯一可能的答案.没有规则说答案应该只是文字和积极的. (2认同)