views:

207

answers:

4

For the most part, when I want to display some HTML code to be actually rendered I would use a 'close PHP' tag, write the HTML, then open the PHP again. eg

<?php
// some php code
?>
<p>HTML that I want displayed</p>
<?php
// more php code
?>

But I have seen lots of people who would just use echo instead, so they would have done the above something like

<?php
// some php code
echo("<p>HTML that I want displayed</p>");
// more php code
?>

Is their any performance hit for dropping out and back in like that? I would assume not as the PHP engine would have to process the entire file either way.

What about when you use the echo function in the way that dose not look like a function, eg

echo "<p>HTML that I want displayed</p>"

I would hope that this is purely a matter of taste, but I would like to know if I was missing out on something. I personally find the first way preferable (dropping out of PHP then back in) as it helps draw a clear distinction between PHP and HTML and also lets you make use of code highlighting and hinting for your HTML, which is always handy.

+1  A: 

No, there's no performance increase that would be visible.

Sometimes its just simply easier to output content using echo (for example, when inside a while or for loop) than to close the php tag.

Eduardo Scoz
+6  A: 

The first type is preferable, exactly for the reasons you mentioned.

Actually, echoing out whole chunks of html is considered bad practice.

WishCow
It's ok to echo out HTML as long as it's really short like a simple link or so ...
Techpriester
Echoing out a simple link would mean that you have to deal with ' " escaping. echo '<a href="' . $link . '">' . $caption . '</a>'; If you use " for delimiters, then you have to escape at the attributes instead of the variables. It's ugly.
WishCow
I use echo for inline coding (<?php echo "hello world" ?>). If you want to create larger dynamic sites then use the first option
Neale
A: 

I think there's a preprocessor which converts the same form into the second. That's what happens in ASP.NET, anyway. And in both ASP.NET and classic ASP, loops can actually stretch across raw-HTML regions.

Ben Voigt
what do you mean? The is only 'processing' in PHP... you may be able to have a tool run through and optimize a spot, but its just interpreted each time its run.
thecoshman
To get any sort of decent performance, the whole thing is going to be parsed into an abstract syntax tree. Interpreting text directly is pitifully slow. For many of these template systems, the parse tree for both forms is the same (a node which represents a string literal and an output command).
Ben Voigt
A: 

There's no performance difference at all.

Just the style that produces the most readable code. Depending on the actual situation that can be either of the two.

But mixing HTML and PHP should be avoided where possible anyway. THis can be accomplished by using a template system for your views.

Techpriester
PHP is a template system.
Frank Farmer
Exactly, no need to implement another layer on top of it for templating.
WishCow