Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 periodically throughout the documents.

I know there may be some readability issues but I'm not to worried about that.

Thanks all!

Example 1

echo '<html>',      '<body>',      'The content of the ',$container,' element is displayed in your ', $other_container,      '</body>',      '</html>'; 

OR

<html> <body> The content of the <?php echo $container; ?> element is displayed in your <?php echo $other_container; ?> </body> </html> 
like image 803
William T Wild Avatar asked Feb 03 '09 00:02

William T Wild


People also ask

Should you echo HTML in PHP?

It is generally considered bad form to output HTML code from PHP. There are several reasons for this, but the main one is that it is easier, especially in the long run, to maintain code if the HTML and PHP code is separated.

Which is faster echo or print in PHP?

They are both used to output data to the screen. The differences are small: echo has no return value while print has a return value of 1 so it can be used in expressions. echo can take multiple parameters (although such usage is rare) while print can take one argument. echo is marginally faster than print .

What can I use instead of echo in PHP?

The PHP print Statement You can also use the print statement (an alternative to echo ) to display output to the browser. Like echo the print is also a language construct not a real function. So you can also use it without parentheses like: print or print() .

Why do we use echo in PHP?

The echo is used to display the output of parameters that are passed to it. It displays the outputs of one or more strings separated by commas. The print accepts one argument at a time & cannot be used as a variable function in PHP. The print outputs only the strings.


2 Answers

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>         <td colspan="<?php echo $numCols; ?>">             <?php echo $a; ?>, <?php echo $b; ?>, and <?php echo $c?>         </td>     </tr> </table> 

versus:

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

Or

<?php echo "<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 ); ?> 
like image 141
nickf Avatar answered Sep 19 '22 23:09

nickf


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.

like image 21
Stephane Grenier Avatar answered Sep 23 '22 23:09

Stephane Grenier