Take the 2-minute tour ×

What is more efficient and/or what is better practice, to echo the HTML or have many open and close php tags?

Obviously for big areas of HTML it is sensible to open and close the php tags. What about when dealing with something like generating XML? Should you open and close the php tags with a single echo for each piece of data or use a single echo with the XML tags included in quotations?

share|improve this question
add comment

7 Answers

up vote 19 down vote accepted

From a maintenance perspective, one should have the HTML / XML as separate from the code as possible IMO, so that minor changes can be made easily even by a non-technical person.

The more a homogeneous block the markup is, the cleaner the work.

One way to achieve this is to prepare as much as possible in variables, and using the heredoc syntax:

// Preparation

$var1 = get_value("yxyz");
$var2 = get_url ("abc");
$var3 = ($count = 0 ? "Count is zero" : "Count is not zero");
$var4 = htmlentities(get_value("def"));

// Output  

echo <<<EOT

 <fieldset title="$var4">
   <ul class="$var1">


You will want to use more sensible variable names, of course.

Edit: The link pointed out by @stesch in the comments provides some good arguments towards using a serializer when producing XML, and by extension, even HTML, instead of printing it out as shown above. I don't think a serializer is necessary in every situation, especially from a maintenance standpoint where templates are so much more easy to edit, but the link is well worth a read. HOWTO Avoid Being Called a Bozo When Producing XML

Another big advantage of the separation between logic and content is that if transition to a templating engine, or the introduction of caching becomes necessary one day, it's almost painless to implement because logic and code are already separated.

share|improve this answer
I didn't even know of the existence of heredoc - This is why I love stackoverflow... So much knowledge being shared. Thanks –  Mark Feb 7 '10 at 13:46
Only bozos print XML. :-( –  stesch Feb 7 '10 at 13:57
@stesch: true, a HTML example may have been more appropriate. On the other hand, I have no problems with generating small XML snippets that way. And why should I? –  Pekka 웃 Feb 7 '10 at 14:39
@Pekka: hsivonen.iki.fi/producing-xml –  stesch Feb 7 '10 at 16:37
@stesch, all good points, especially the one about pretty printing. I still think that this way of producing XML is fine in many situations, and a serializer often overkill. Still, I'm adding a HTML example and adding a comment, including your link, to the XML one. –  Pekka 웃 Feb 7 '10 at 17:56
show 2 more comments

PHP solves this problem by what is known as heredocs. Check it out please.


  echo <<<EOD
  <td class="itemname">{$k}s</td>
  <td class="price">{$v}/kg</td>

Note: The heredoc identifer (EOD in this example) must not have any spaces or indentation.

share|improve this answer
+1 for proper use of curly braces –  Joe Philllips Feb 7 '10 at 22:45
@D03boy: Thanks buddy :) –  Sarfraz Feb 9 '10 at 5:59
add comment

Whichever makes sense to you. The performance difference is marginal, even if a large echo is faster.

But an echo of a big string is hard to read and more <?php echo $this->that; ?> tell a story :)

share|improve this answer
+1 and besides, you will probably use some kind of cache if you have performance problems, which should eliminate the need for such distinction. –  Itay Moav -Malimovka Feb 7 '10 at 13:37
add comment

echo sends its argument further down the request processing chain, and eventually this string is sent to the client through a say, network socket. Depending on how the echo works in conjunction with underlying software layers (e.g. webserver), sometimes your script may be able to execute faster than it can push data to the client. Without output buffering, that is. With output buffering, you trade memory to gain speed - you echos are faster because they accumulate in a memory buffer. But only if there is no implicit buffering going on. One'll have to inspect Apache source code to see how does it treat PHPs stdout data.

That said, anything below is true for output buffering enabled scripts only, since without it the more data you attempt to push at once the longer you have to wait (the client has to receive and acknowledge it, by ways of TCP!).

It is more efficient to send a large string at once than do N echos concatenating output. By similar logic, it is more efficient for the interpreter to enter the PHP code block (PHP processing instruction in SGML/XML markup) once than enter and exit it many times.

As for me, I assemble my markup not with echo, but using XML DOM API. This is also in accordance with the article linked above. (I reprint the link: http://hsivonen.iki.fi/producing-xml/) This also answers the question whether to use one or many PHP tags. Use one tag which is your entire script, let it assemble the resulting markup and send it to the client.

share|improve this answer
add comment

Personally I tend to prefer what looks the best as code readability is very important, particularly in a team environment. In terms of best practice I'm afraid I'm not certain however it is usually best practice to optimize last meaning that you should write it for readability first and then if you encounter speed issues do some refactoring.

Any issues you have with efficiency are likely to be elsewhere in your code unless you are doing millions of echo's.

Another thing to consider is the use of an MVC to separate your "views" from all of your business logic which is a very clean way to code. Using a template framework such as smarty can take this one step further leading to epic win.

share|improve this answer
add comment

Whatever you do, don't print XML!

See HOWTO Avoid Being Called a Bozo When Producing XML

share|improve this answer
+1, funny to see two bozos have already been touched by the title. ;) –  just somebody Feb 7 '10 at 20:22
add comment

I've made myself the same question long time ago and came up with the same answer, it's not a considerable difference. I deduct this answer with this test:

        $r = benchmark_functions(

    function output_embeed($i) {
        ?>test <?php echo $i; ?> :)<?

    function output_single_quote($i) {
        echo 'test '.$i.' :)';

    function output_double_quote($i) {
        echo "test $i :)";

    function benchmark_functions($functions, $amount=1000) {
        if (!is_array($functions)||!$functions)
        $result = array();
        foreach ($functions as $function)
            if (!function_exists($function))
        foreach ($functions as $idx=>$function) {
            $start = microtime(true);
            for ($i=0;$i<$amount;$i++) {
            $time = microtime(true) - $start;
            $result[$idx.'_'.$function] = $time;
share|improve this answer
add comment

Your Answer


