Mixed output and timing

Right, so I learned to code mixed html/php the sloppy way, switching back and forth whenever the mood struck. Lately, I’ve been realizing what a pain it is to figure out what’s where when something needs to
change…. so I switched to just putting all my output into a variable, then outputting it in a pesudo-template at the end of the script.
I’ve started working with larger datasets, so I added a timer function to display the time it took to generate each page in the footer…. I ran my old page a few times, and saw consisted 4+ second generation times. I changed it to the pseudo-template, and I’m getting consistent .2 second times.

I also read of a difference in using
“stuff $php_variable stuff”, which is slower than
“stuff ” . $php_variable . ” stuff”, so I changed that where
appropriate as well. Additionally, ‘stuff’ is also faster, but by a neglible amount.

Here’s the old code:
[code]
while ($myrow = mysql_fetch_array($result)) {
if (mysql_errno() != 0) {
echo “Error ” . mysql_errno() .”

“;
} else { // end if (mysql_errno() != 0)
?>


“;
} else { // end if (mysql_errno() != 0)
$out .= “

“;
$out .= ”

” . $myrow[“id”] . ”
“;
$out .= ”

” . $myrow[“title”] . ”
“;
$out .= ”

” . $myrow[“artist”] . ”
“;
$out .= ”

” . $myrow[“bpm”] . ”
“;
$out .= ”

” . $myrow[“genre”] . ”
“;
$out .= ”

” . $myrow[“stuff”] . ”
“;
$out .= ”

“;
} // end else
} // end while ($myrow = mysql_fetch_array($result))
[/code]
This was for about 425 rows, I believe.

I had no idea it made THAT much of a difference… Guess I’ll be using that on everything now… If not switching over to SMARTY or some other true templating system.

22 thoughts on “Mixed output and timing

  1. Mixed output and timing
    Thanks for posting your findings. I personally stopped this sort of thing despite the speed increase. I’ve moved away from putting any HTML in echo or php strings altogether for a couple of reasons. The first being that it makes my applications editable by someone who doesn’t know PHP. i.e. I build my ugly programs and then later hire an HTML designer to make it look pretty. If all of the HTML code is in a bunch of PHP strings the HTML designer will have a heart attack. Another reason is simply that it makes it easier for me to read my own code because I’m a color highlighted syntax junky and my editors won’t highly my html code if it is in a PHP string. Also I don’t have to add slashes in front of all my quotes like $output = "<div align="center">"; which I think makes a bit of a mess.

    But if speed is a big issue for you then you may very well be better off doing it your way. I’m certainly not going to tell you which way you should do it. I just wanted to add an alternate viewpoint. 🙂

    – Doug

    • Mixed output and timing
      I agree to some extent with your reasoning… However, when working with dynamic pages and large result sets…. It’s worth some small pain to go ahead and use some sort of pseudo-templating system, IMO.

      The other nice thing can do is allow for MORE separation of code and presentation… I didn’t post the whole script, obviously, but most of the presentation (headers, footers, nav tables, etc) is at the END, with the $out variable placed where it needs to be. This makes whoever’s doing JUST the presentation much happier, since most of it is right there (away from the php code).

      Granted, the mixing for data loops is elsewhere – but that’s minimal, once the rest of the page structure is set. You could also, potentially, have a few different output variables set up (I do a couple others for navigation in-page and site-wide, one of which is also pulled from the DB) and further help the presentation/data separation.

      If you’re interested, I’ll post more example code to demonstrate.



      ThatGuy
      http://www.ddrkc.com

      • Mixed output and timing
        Ok. I guess it makes sense in many situations, especially if you use stylesheets liberally. And as long as the graphic designer isn’t going to need to edit the actual content (and he probably wouldn’t) then doing a template might make it even easier for him/her to work with. Still, in a lot of cases I see a graphic designer wanting to have access to all the html tags around the content (especially in the case of tables) and being totally thrown off by all the tr, td, and font tags being seperate from the rest of the html. I’d think liberal use of stylesheets is a must in this situation.

        – Doug

        • Mixed output and timing
          Well, font tags shouldn’t used apart from css anyway… So I agree there.

          With a minimal bit of work on the coder’s end, the design will be pretty much intact… You’d replace the data cells in each table with an output variable, and move the row design to that variable in your code loops…. Not a big deal, really. True, the updating isn’t QUITE so convenient when the design changes… but it’s still pretty minimal, and the speed and code separation benefits outweigh that small pain…

          It’s much harder when you’re buiding the page as you go, and trying to figure out which bit to tweak in the mess, IMO.



          ThatGuy
          http://www.ddrkc.com

  2. Re: Zend
    Being a Linux user, this is my PHP IDE of choice. It is pricey, but it is worth every penny if you work with projects of nearly any descent size.

    Now is a great time to check it out: ZDE 4 is in beta and they have some great incentives for beta participants!!

Leave a Reply

Your email address will not be published. Required fields are marked *