tags:

views:

717

answers:

9

Is there any speed difference between these two versions?

<?php echo $var; ?>

<?=$var?>

Which do you recommend, and why?

+2  A: 

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

John Rasch
+5  A: 

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 Davis
When you have billions of runs you won't/shouldn't be using PHP anyways.
tstenner
@tstenner: Facebook disagrees with you.
TheTXI
Throw enough hardware at it... There are times where developer time may be more expensive than more hardware, and refactoring all the code for better performance isn't wise if you can simply scale through hardware. Still, they use php optimizers...
Adam Davis
+5  A: 

Performance wise it is insignificant.

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

Paolo Bergantino
+1 for exact same thoughts
David
+5  A: 

In next version of php short tag support will be removed so try to avoid this and rewrite the code to the '<?php echo' format

Shtirlic
I did not know this, thanks for the heads-up
John Rasch
Yet another genious idea from those at PHP... :|
PintSizedCat
As far as I know, it is not getting removed just getting turned off by default. There is a difference.
Paolo Bergantino
Which makes the answer more or less FUD. Nice.
chaos
+5  A: 

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.

Bart S.
+11  A: 

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

vartec
You'r right, I like to write portable code..thank you all 4 responses.
grilix
I always use the short form. If ever I need to run code where it's not enabled or when I need to for XML (neither of which has EVER happened), it's a simple case of s/<?/<?php/ and s/<?php=/<?php echo /
cletus
@cletus: with all due respect, you've got no frikin' idea.
vartec
+1  A: 

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.

bobince
At this point, I have all variables clean and safe :).. So I only have to print it out..
grilix
@bobinceThat is a severe case of paranoia... do you use htmlspecialchars() on ALL your vars?
jcinacio
@jcinacio — If the variable is being echoed to the client and hasn't been intentionally constructed to contain HTML, yes, you really should be encoding it first. It's potentially a *very* serious security problem.
Ben Blank
@jcinacio: yes, of course (except where I explicitly need to output raw HTML, which is relatively rare). All output of text to HTML has to be escaped, same as all output of text into SQL. Miss just one and you have a security hole, so it's easiest to always use it even for known-safe variables.
bobince
+1  A: 

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.

Bob Fanger
+1  A: 

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.