tags:

views:

200

answers:

10

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 if ($output) { ?>
  <h2><?=$output;?></h2>
<?php } ?>

and

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

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 } ?>
Ish Kumar
+2  A: 

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).

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.

Josh Smeaton
+3  A: 

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.

soulmerge
Don't generalize.
orlandu63
I'm not optimising code and I would ignore an issue like this if i were and concentrate on all the dodgy MySQL statements i'd written :P. I just wondered if either of them were prefered by compilers as I find myself writing both types of statement.
ewengcameron
@ewen ... I think PHP is interpreted, not compiled.
alex
true / i am retarded. need more caffeine
ewengcameron
@orlandu: Ok, changed wording
soulmerge
+1  A: 

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

Performance is negligible in both cases.

Mario
+5  A: 

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

grawity
++ for short_open_tags
Martin K.
yeah, i should have made that line <?php echo ... ; ?> instead. copied it off an old site with short_open_tag on
ewengcameron
+1 I agree, however I am yet to see a setup that has them disabled.
alex
+8  A: 

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

Florian
++ for readable code
Martin K.
+4  A: 

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)

truppo
+5  A: 

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.

Adam Wright
+1  A: 

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

mP
+1  A: 

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... :)

time: 2.66 microseconds

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

time: 1.65 microseconds

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

time: 1.65 microseconds

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

time: 1.64 microseconds

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