Skip to content Skip to sidebar Skip to footer

Escape Html To Php Or Use Echo? Which Is Better?

In terms of performance , what would be better. Using PHP to echo all the HTML output so I can pepper it with the various bits of working code and variables or escape HTML to php p

Solution 1:

It's all about which you find the most readable. Of course, this will vary with each situation. If you were doing an entire page, and there were large sections which did not have any PHP in it, then I'd break out of PHP and just write the plain HTML, whereas if there was a section which had a lot of PHP variables, I'd do it all in PHP.

For example:

<table><tr><tdcolspan="<?phpecho$numCols; ?>"><?phpecho$a; ?>, <?phpecho$b; ?>, and <?phpecho$c?></td></tr></table>

versus:

<?phpecho"<table>"
    . "<tr>"
    .    "<td colspan=\"" . $numCols . "\">"
    .        $a . ", " . $b . " and " . $c
    .    "</td>"
    . "</tr>"
    . "</table>"
; ?>

Or

<?phpecho"<table>
         <tr>
            <td colspan='{$numCols}'>
               {$a}, {$b}, and {$c}
            </td>
         </tr>
      </table>";
?>

Also don't forget about printf

<?php
printf("<table>"
    . "<tr>"
    .    "<td colspan=\"%d\">%s, %s and %s</td>"
    . "</tr>"
    . "</table>"
    , $numCols
    , $a
    , $b
    , $c
);
?>

Solution 2:

Whichever is easiest to read.

The performance difference is pretty negligible compared to almost any other issue you'll encounter. Definitely readability is hands down the first issue here.

Solution 3:

Let's cite the manual:

The example given here is contrived, of course, but for outputting large blocks of text, dropping out of PHP parsing mode is generally more efficient than sending all of the text through echo() or print().


But please listen to the others and avoid this sort of micro optimization and choose the one that is more legible.

Can I propose a third solution? Have you heard of template engines? They help you create a clear separation between code and presentation which usually leads to cleaner code which is easier to maintain. A popular such template engine is e.g. smarty, but there are hundreds with different strengths.

Solution 4:

It's pretty irrelevant to the overall speed of your app which one you go with.

That being said, I know you said you don't care, but please use the 2nd one as it's about a million times easier to read. And readability is huge.

Solution 5:

I don't know about the performance of this, but within PHP you can also use what I believe is called a "Heredoc", which I think helps with readability within this sort of output.

Nickf's example:

<table><tr><tdcolspan="<?phpecho$numCols; ?>"><?phpecho$a; ?>, <?phpecho$b; ?>, and <?phpecho$c?></td></tr></table>

becomes:

<?phpecho<<<EOT
<table>
    <tr>
        <td colspan="$numCols">
            $a , $b, and  $c
        </td>
    </tr>
</table>

EOT;

?>

I do believe that ultimately readability is a subjective thing, so your mileage may vary!

Ruth

Post a Comment for "Escape Html To Php Or Use Echo? Which Is Better?"