Fun with SimpleXML

Ok, I’ve read about SimpleXML. I’ve looked at examples, and articles, and books and said…”Oh, that’s nice.” But tonight, I decided to whip up a quick and dirty RSS parser with it.

It took about 5 minutes (not that it does much), and I had a very simple rss feed reader for my bookmarks on [url=http://del.icio.us]del.icio.us[/url]. When I was done, all I could say: “Oh, that’s REALLY nice!”

Read on for a simple simpleXML example, some links and more.

As far as the code goes:
[code]
under the root tag.
foreach( $xml->item as $item ){
echo “

phppatterns.com

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.

PHP CLI Ncurses

Link: Using Ncurses in PHP

PHP Command Line Interface has made some vast improvements over the past few months. One of which is Ncurses functionality. I read this article at Zend.com and thought it was very cool. It allows you to format the text on the screen. The article has screenshots to help you understand what it really does.

Before everyone tackles me for using PHP for shell scripts, I’d like to present my reasoning. Sure, even with the advances PHP has made recently, PERL is still probably a better option for shell scripting. Quite frankly, I don’t have the time to learn all the ins and outs of PERL just to write a simple script to dump some database records. I already know how to do that in PHP, so why not use PHP? I think sometimes people underestimate the value of having one language that can do everything you need. It keeps syntax confusion to a minimum and allows me to study the more advanced aspects of the language instead of just knowing the basics of multiple languages. Not to mention it cuts development time by ungodly margins.

I urge you to give PHP a try for shell scripts (That is, unless you are already a PERL god, which I am not).