Is there a speed difference between <?php echo

2019-01-15 07:20发布

Is there any speed difference between these two versions?

<?php echo $var; ?>

<?=$var?>

Which do you recommend, and why?

9条回答
来,给爷笑一个
2楼-- · 2019-01-15 07:53

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.

查看更多
干净又极端
3楼-- · 2019-01-15 07:59

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.

-Adam

查看更多
来,给爷笑一个
4楼-- · 2019-01-15 07:59

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.

查看更多
老娘就宠你
5楼-- · 2019-01-15 08:01

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

<?=$var?> requires short tags activated. 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

查看更多
神经病院院长
6楼-- · 2019-01-15 08:08

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.

查看更多
手持菜刀,她持情操
7楼-- · 2019-01-15 08:10

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.

查看更多
登录 后发表回答