Page 1 of 1

Embedded HTML versus echo or print

Posted: Fri Oct 24, 2003 9:39 am
by mhorner
I have a question that I believe I read something on this at one point but I can't recall where.

My question really is in regards to speed/optimization of the code rather than a style issue. (But style comments are wanted also :) )

What is better in terms of optimization in these two different code blocks:

Code: Select all

<?php
  if (1) {
     echo "<H1>Hello World.</H1>";
  }
?>
And

Code: Select all

<?php 
  if (1) {
?>
<H1>Hello World.</H1>
<?php
  }
?>
In the second case does the PHP interpretter just ignore the break and continue processing the rest of the code? Essentially leaving the HTML alone and moving passed it without causing any CPU/Mem hit?

Obviously if you have a lot of case 1 versus case 2 you actually spending more time processing function calls, but is there anywhere anyone knows of that documents this information? Anyone have any ideas?

Thanks for any help on this!
Matt

Posted: Fri Oct 24, 2003 10:39 am
by devork
I have also seen in many project using

Code: Select all

&lt;?php
echo "&lt;HTMLTAGS&gt; ";
?&gt;

Posted: Fri Oct 24, 2003 11:36 am
by m3rajk
i've heard that the second style of the initial post of the thread actually makes the parser have more to do in parsing before sending.

i know the echo "blah blah"; method can be tiresome for long blocks, that's why people use heredocs (which i rmember someone posting saying that the parser is actually optimized for... not sure the validity of that)


just go to php.net and look up echo

heredocs are covered there.


btw: if you don't have php.net bookmarked: bookmark it

Posted: Fri Oct 24, 2003 12:05 pm
by Nay
Did someone say heredoc? :lol:..

Anyhow, I guess, with the second style, you can't do anything else but to output, unless you want to reopen and close tags along the script - which is quite troublesome.

-Nay

Posted: Fri Oct 24, 2003 12:17 pm
by mhorner
Thank you all for your reply. Much appreciated!

I knew that PERL had heredocs, however i didn't know that PHP also had them.

I wasn't sure whether the second scheme actually was more intensive, however doing some benchmarking would have been a quick and easy approach, but I figured someone has dealt with this already.

Thanks again!

Posted: Fri Oct 24, 2003 2:58 pm
by d3ad1ysp0rk
You could always get a PHP editor with a replace feature :P