1

I have two bits of code that do the same thing:

foreach ($pdo as $var) { ?>
    <div class="someclass"> <?php
        echo $var["column"]; ?>
    </div> <?php
}
//or
foreach ($pdo as $var) {
    echo "<div class='someclass'>";
    echo $var["column"];
    echo "</div>";
}

The first is quicker to type, but the second looks cleaner. Is there any kind of performance difference between remaining in PHP versus weaving in and out through PHP and HTML? If there is no performance difference, which is 'correct', as in most commonly used?

5
  • 2
    It won't matter. The difference will be in the microseconds. Commented Oct 24, 2013 at 22:31
  • 1
    A lot of people consider it bad practice to store html in php strings Commented Oct 24, 2013 at 22:34
  • 1
    Note: echo 'something'; is actually quicker than echo "something"; - when the outer quote marks are doubles ("..") then the contents is evaluated. So, $var='blah-blah'; echo "I say: $var"; gives a different result to $var='blah-blah'; echo 'I say: $var'; Commented Oct 25, 2013 at 0:02
  • If a variable is not involved, is that considered proper still? echo "<div class='someclass'>"; will work fine, but echo "<div class="someclass">"; will not. Commented Oct 25, 2013 at 0:05
  • 1
    @riista - yes, it is still considered proper - and it's still faster, since there's no need to examine the string before deciding that no substitution is required. You could use either of these two: echo '<div class="someClass">'; or echo '<div class=\'someClass\'>'; - i.e ensure the outer-most quotes are singles, then use doubles inside, or you could escape the internal singles with a slash (\). - the first one is faster since there's no need to parse the escape slash. Commented Oct 25, 2013 at 11:25

1 Answer 1

1

I believe the times should be identical. ?> foo <?php and echo 'foo'; are equivalent in PHP, just different syntactically. It's like asking the performance difference between if(condition) { } and if(condition) endif;.

Note that ' and " are different, so using single-quoted strings with echo might be slightly faster because it won't be inspecting the strings for $placeholders.

Of course, as @Pekka said, the performance difference is negligible, and if you truly care about performance then you won't be using PHP in the first place.

Sign up to request clarification or add additional context in comments.

2 Comments

For some, including me, PHP is just simpler to use and learn than something like Python or Perl (haven't tried my hand at Ruby yet). I know about fractal of bad design, dual-clawed hammer, etc complaints about PHP, but surely the performance isn't that bad for PHP when compared to alternatives.
Right, PHP isn't bad, - but for high-performance, large-scale applications (like Facebook) they use a special PHP execution engine with a whole lot of stuff written in C++. I think Wikipedia uses the stock PHP engine, but I'm not certain.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.