You are using a browser which is not compatible with CSS (for more information, see Tara's tutorial). Because of this, it is possible that our website may not appear correctly in your browser. We apologise for the inconvenience, and recommend you upgrade your browser to one which is compatible with CSS. For more information, please visit our Browser Upgrade page.

4WebHelp

To echo or not to echo by Daniel

Last updated: 02/09/2004
Biography: Daniel has been interested in web design since 1999, when he first started designing a personal website. Since then, he has learnt much, and is interested in new web standards like XHTML and CSS, and in PHP.

Daniel is responsible for managing the 4WebHelp Forums, along with the forum moderators. He should be contacted for any modifications to your forum account (which you cannot do through the phpBB interface) and for any issues you may have with moderation.
See 10 more tutorials by Daniel

Something very important when coding a substantial script, is the time it takes to be processed by PHP. Some people seem to forget this, and write scripts which take an unnecessarily long time to be parsed.

Now, I'm not going to go through all the ways to make your scripts more efficient; I'll just focus on one particular thing: the use of the echo(); command. This command is overused in PHP, often by ignorance.

The alternative to echo();, when all you want to output is pure HTML, is "inline HTML".

Did you know that echo(); is twice as slow as inline HTML?

Here's an example of the improper use of echo();:

<?php
if ($variable=="bla"
{
  echo (
"<b>bla HTML Code</b>");
}
?>

Here's an example of "inline HTML":

<?php
if ($variable=="bla")
{
?>
<b>bla HTML Code</b>
<?
}
?>

Not only is this approach faster, it will also save putting those backslashes in front of " and '.

© 4WebHelp and Daniel

Comments on this tutorial
Name: Tony FreemanEmail Tony dot Freeman at gmail dot com
sorry was not code highlighted


oh and i also hate when people use

if($v == 'no') {
                       echo 'why not?';
                     } else
           if( $v == 'yes' ) {
                                       echo 'oh! yes!';
           } else {
                       echo 'what kind of answer is that?'; }
but yea anyway here is the code below highlighted. Smile

<?php
$v = $_GET['v'];
switch ($v) {
      case "1":
           echo '<b>Yeeeehaa!  Let's get it on!</b>';
      break;
      case "0":
           echo '<i>Doh! Not now!</i>';
      break;
}
?>
Name: Tony FreemanEmail tony dot freeman at gmail dot com
I would use switch instead of if.

<?php
$v = $_GET['v'];
switch ($v) {
       case "1":
            echo '<b>Yeeeehaa!  Let's get it on!</b>';
       break;
       case "0":
            echo '<i>Doh! Not now!</i>';
       break;
}
?>

Smile to meny if's drive me nuts. you can also add more case values to that but in order to exit that case you must end it with a break; Smile have fun.
Name: JeremyEmail jeremy_swinarton at netscape dot ca
I really don't see the point in using inline HTML unless you plan to echo a large block of html, such as a form or something.  If I was only echoing one line of HTML, the speed difference wouldn't be that substantial, and I would use echo to save me and others working on the script time.
Name: DraxasEmail none
You call this a tutorial?

There is no benchmarking at all, so there are really no good reasons for that because php scripts are executed by php and html is also filtered by php!
Name: JasonEmail none
To a point i agree... and follow the advice given in this tutorial.. but let it be said that echo is only slower when you user echo ""; if you user echo ''; (single quotes) PHP does not bother to parse for variables and printing the echo is litterally the same as printing the HTML.

Someone may begin to argue by saying, "your wrong because the php engine still has to look at echo and echo it" but that is incorrect because the entire script goes through the php engine anyway.

single quotes = fast, double quotes = slow, <<< = slow.
Hope that helps.
J
Name: MariusEmail marius at o2mlab dot com
Just a simple hint on how to prevent getting a messy echo-code by slashing out special characters:
<?php
echo "<a href=\"somelink.php\">";
?>
may easily be replaced with the other string-applying element:
<?php
echo '<a href="somelin.php">';
?>

For the ones who allways get the nice little error when writing HTML with echo Razz
Name: RuggieEmail ruggie1of1 at hotmail dot com
No what Daniel wrote is not complete nonsense.  If you only wanted to print the message "<i>Doh! Not now!</i>" ONLY when $v is 0 then the code would look like this:


<?php
if ($v==1) {
?>
<b>Yeeeehaa!  Let's get it on!</b>
<?php
}
elseif ($v==0) {
?>
<i>Doh! Not now!</i>"
<?php
}
?>
Name: DaskarEmail none
Quote:
So what would be wrong with this?
<?php
if ($v==1) {
?>
<b>Yeeeehaa!  Let's get it on!</b>
<?php
}
else {
?>
<i>Doh! Not now!</i>"
<?php
}
?>

That code doesn't need the many different opening and closing tags. Just use:

<?php
if ($v=="1")
{
 echo ('<b>Yeeeehaa!  Let's get it on!</b>}');
}
else
{
 echo ('<i>Doh! Not now!</i>');
}
?>

That just makes the coding easier on the eyes.
((NOTE: I am just getting into PHP - correct me if the code I have posted won't work, but I think it will...))

~Daskar
Name: MikeEmail none
"Thats Me" what planet was you born on.  What you have written is total nonsense. If you clearly know nothing about PHP then why bother wasting people time posting a message with no backing of knowledge.
Name: suzieEmail none
that usage of the colon in the if stmt has really cleaned up my code, thanks for that
Name: without using the brackets {}Email Christophe dot Marand at univ-nancy2 dot fr
This will do also replacing the brackets by a ":"

<?php
if ($v==1) : //note the ":"
?>
<b>Yeeeehaa!  Let's get it on!</b>
<?
else :  //note the ":"
?>
<i>Doh! Not now!</i>
<?
endif
?>
Name: Moises KirschEmail none
As far as I know it is also faster to use single quotes instead of double quotes because when you use double quotes the echo command evaluates everything.

So it is faster to use:

echo ('Hello');

than

echo ("Hello");
Name: NickEmail none
oh yeah..

"what about readability na dproper PHP coding?"

haha@ the unintended irony of that sentence Razz
Name: NickEmail none
this tutorials a good point well put. i hope (for their own benefit) the person who called it nonsense took notice of the usage of curly brackets to offer the choice of extending the if condition across the gap between php sections so the non-php section is only used if the condition is met.

anyway, thanks Daniel!
Name: DanielEmail use contact form
Quote:
I though that if you were going to drop out of php in the middle of a control structure you were supposed to use the alternate syntax of if

That is a personal coding choice. I myself have not read anything which says you have to do that.

Quote:
If you are populating a table for instance from a database, why would I use inline HTML and then have 12 <?=$text;?> in the middle of the HTML then???

Let's face it, in the end most of the HTML output by a PHP script is usually static. However for the HTML that contains variables, you may find it easier to use an echo statement. I personally find it "cleaner" to keep my HTML seperate (this is going one step further towards the seperation of code and layout, although much more can be done).
Name: EqualHateEmail equalhate at planethalflife dot com
what about readability na dproper PHP coding?
I though that if you were going to drop out of php in the middle of a control structure you were supposed to use the alternate syntax of if :
elseif:
endif:
instead of encapsulating with curly brackets.
my other comment is readability.
If you are populating a table for instance from a database, why would I use inline HTML and then have 12 <?=$text;?> in the middle of the HTML then???
just curious
Name: DanielEmail use contact form
Yes, I would tend to agree with you that if you've got very little text to echo, it's not worth getting out of the code. However, anything longer than about 10 chars should IMO not be in an echo command. I actually find it cleaner because you don't have to worry about special characters like ' or ".
Name: some guyEmail none
I think its a bit easier and cleaner (IMO) just to write echo for a simple line of text, just if your doing a whole table layout or something, then you can get out or use
echo<<< END
...code
END;
Name: DanielEmail use contact form
So what would be wrong with this?

<?php
if ($v==1) {
?>
<b>Yeeeehaa!  Let's get it on!</b>
<?php
}
else {
?>
<i>Doh! Not now!</i>"
<?php
}
?>

Add a new comment

This page is © Copyright 2002-2008, 4WebHelp. It may not be reproduced without 4WebHelp's prior permission.