Embedded HTML versus echo or print

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mhorner
Forum Newbie
Posts: 5
Joined: Fri Oct 17, 2003 12:55 pm
Location: Wauwatosa, WI
Contact:

Embedded HTML versus echo or print

Post 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
User avatar
devork
Forum Contributor
Posts: 213
Joined: Fri Aug 08, 2003 6:44 am
Location: p(h) developer's network

Post by devork »

I have also seen in many project using

Code: Select all

&lt;?php
echo "&lt;HTMLTAGS&gt; ";
?&gt;
m3rajk
DevNet Resident
Posts: 1191
Joined: Mon Jun 02, 2003 3:37 pm

Post 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
Nay
Forum Regular
Posts: 951
Joined: Fri Jun 20, 2003 11:03 am
Location: Brisbane, Australia

Post 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
mhorner
Forum Newbie
Posts: 5
Joined: Fri Oct 17, 2003 12:55 pm
Location: Wauwatosa, WI
Contact:

Post 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!
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post by d3ad1ysp0rk »

You could always get a PHP editor with a replace feature :P
Post Reply