Is there a speed difference between <?php echo $var; ?> and <?=$var?>?

筅森魡賤 提交于 2019-11-28 00:07:42

问题


Is there any speed difference between these two versions?

<?php echo $var; ?>

<?=$var?>

Which do you recommend, and why?


回答1:


Performance difference is insignificant. Moreover, with use of APC, performance difference is zero, null, nada.

Short tags are problematic within XML, because <? is also markup for XML processing tag. So if you're writing code that should be portable, use the long form.

See short_open_tag description in http://www.php.net/manual/en/ini.core.php




回答2:


Technically the parser has to parse every character of the longer version, and there's a few more characters for every transfer.

If your webserver doesn't "pre-compile" (ie: cache tokenized PHP pages) then there is a slight performance difference. This should be insignificant except, perhaps, when you start talking about billions of runs.




回答3:


Performance wise it is insignificant.

Proper usage says to use the longer one, as it is more portable. Personally? I do the shorter one.




回答4:


in php 5.3 short tag ASP-style <% %> support will be deprecated, try to avoid this and rewrite the code to the '<?php echo' format, because u cant use <?xml ?> inline for example.




回答5:


No, they are identical. If you like typing a lot use <?php echo $var; ?>, otherwise just save time with <?=$var?>.




回答6:


I think the second one requires the short_open_tag (in PHP.ini) to be set to true.

Meaning there is a chance it's turned off on some webservers.




回答7:


Which do you recommend

Neither, unless you really want to allow HTML injection. (99% of the time, you don't.)

<?php echo htmlspecialchars($var); ?>

Or define a function that does echo(htmlspecialchars($arg)) with a shorter name to avoid all that typing.




回答8:


The speed difference depends on how fast you can type those 9 extra characters.
It can also improve the readability of your code, but this is debatable.

If your talking about execution-speed there is no noticable difference.




回答9:


Don't try to optimize with these, it's useless. Instead, deactivate allow_short_tags (because of problems when loading XML files) and write clean, readable and understandable code.

Even if there may be a slight difference (which is definitely lower than 10%), it's useles to optimize with it. If your scripts are slow, look at your loops first. Most of the time you can win a lot more performance by optimizing the programms flow than by using strange syntax.




回答10:


These two lines of code are identical.

I'm adding a late answer because nobody has demonstrated this yet, but the answer is unequivocally no, there is no performance difference specifically because there is no difference at all in how PHP executes these two lines of code.

The interpreter sees the identical code in both cases. The parser produces the exact same AST, because <?= is fundamentally identical to <?php echo. There is no difference in the instructions the interpreter runs when you write <?= vs <?php echo.

By installing php-ast you can examine the AST produced by both lines of code.

Given these two cases...

# CASE 1
<?php echo $i %>

# CASE 2
<?= $i ?>

The abstract syntax tree for both is identical:

case 1
AST_STMT_LIST
    0: AST_ECHO
        expr: AST_VAR
            name: "i"
case 2
AST_STMT_LIST
    0: AST_ECHO
        expr: AST_VAR
            name: "i"

This means PHP cannot tell the difference between these at run time, never mind experiencing some kind of performance difference.

The code to produce this output is as follows, and uses util.php:

<?php
require('util.php');

echo "case 1\n";
echo ast_dump(ast\parse_code('<?php echo $i ?>', $version=50));

echo "\n";

echo "case 2\n";
echo ast_dump(ast\parse_code('<?= $i ?>', $version=50));

echo "\n";

Optimization is irrelevant here. The choice comes down to personal preference, especially since <?= is always available, has nothing to do with short tags, has never been deprecated and is not slated to be removed from the language.



来源:https://stackoverflow.com/questions/662891/is-there-a-speed-difference-between-php-echo-var-and-var

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!