Practical Web Programming

Thursday, November 06, 2008

PHP: How to Print/Echo HTML Tags More Effeciently

PHP is very good at handling strings. But sometimes you have to help PHP do it's job more efficiently. In my recent web development job, I noticed that I always use PHP to print HTML tags. Here's an example of how I would do it before.


<?php
echo "<table>";
echo "<tr>";
echo "<td>"."How to print/echo HTML tags in PHP."."</td>";
echo "</tr>";
echo "</table>";
?>


Now, here's how I do it now which I think is more efficient and HTML readable.

<?php
echo "
<table
<tr>
<td>"."How to Print/Echo HTML tags in PHP More Effeciently"."</td>
</tr>
</table>
";
?>


Notice the difference? In the first PHP sourcecode, I use the echo five times, while in the second one, I use only once.

Using echo to print HTML tags is not expensive in terms of server resources, but if you're using it the way I did in the first example for all your pages and and you have thousands of users to your website, you'll see the difference.

0 comments:

Recent Post