Matt's Blog: while { coding }
    Back to Matt's Homepage   |   Hire Matt

Why PHP Ain’t Going Nowhere

Yes, the title of this post is a double negative. Instead of thinking of it as a grammatical error, think of it as a language menage a trois. Isn’t that better?

Anyway…

My website has needed a facelift for quite some time. I keep putting it off. I still like the basic design enough that it doesn’t bug me when I look at it. I have a sweeping new design in mind, but it will likely be a while before I get to do it.

I still do little nips and tweaks on the current site from time to time. Recently I remembered the tagline generator I used to have on my main page. Every time you’d load the main page, a different tagline would be displayed below my name on the main page. I built it with a simple Perl script and some shtml wizardry. Sadly, it didn’t survive the move to a new web host many years back. I had to start from scratch.

I used PHP. Why PHP? Many of you know I’ve been doing a fair amount of Django work lately. I went the PHP route because – let’s be honest – PHP is everywhere, including on my web host’s server. There’s nothing to set up or mess around with. It’s just there. I’ve said before and I’ll say again, PHP is a lingua franca. It’s the C of the web.

In 10 minutes (no exaggeration) I had code up and working. This isn’t a how-to guide, just a description of how crazy-fast this tiny task got done.

Here’s what I did:

1. I googled for “php ad rotator“. An ad-rotator is just what it sounds like… it’s code that will show a new advertisement on every page load. The “advertisement” will typically consist of text or html, so while the feature is called an “ad rotator”, it’s really a “content rotator”. I know the ad rotator concept from classic ASP back in the day, and figured this must exist in PHP. Sure enough, first item in the search results gave me the code I wanted.

The code wasn’t written the way that I would have written it, but a quick scan showed that it was good enough.

2. My main page was still an html doc, not php. I renamed that sucker to index.php. (For non-index pages this might break inbound links, but if someone is linking to my index.html page instead of the containing directory, that’s their problem.)

3. I pasted the code below where I wanted the tagline to appear in my page. Then I wrapped that up in an HTML comment block and saved it. After a page load and View|Source I got just what I wanted: An error hidden in my HTML comment! The error was that my tagline text file didn’t exist yet.

4. I put my tagline file in place. I put some test taglines in and reloaded. Then I spent a minute debugging, as data showed up in the browser despite my HTML comments surrounding it. Hey, I didn’t say PHP wasn’t a pain in the ass! Using comments wasn’t in the game plan, but I figured the problem out quickly and got it working to my satisfaction while the comments were still in place.

5. I took off the comments and my tagline generator was up and running. Total time was less than 10 minutes.

This is why PHP succeeds and will continue to succeed until something comes along and succeeds better: you can get shit done with it very, very fast.

Of course I wasn’t *totally* done. I had a few bones to pick with the code I had found and so reworked it on the spot. I put the bulk of the code into a function, then I reworked that function until I was happy with it.


function select_tagline($filename)
{
	// Pull in a list of tilde-delimited taglines.
	$taglines = split("~", join('', file($filename) )  );

	// Return a random tagline from the list.
	return $taglines[ rand(0, count($taglines) - 1 ) ];
}

Once the code was better, I ran off and found a few taglines I was semi-happy with and pasted them into my taglines file. Total time from concept to completion? About 30 mins.

Is it perfect? No. I’m not thrilled with the tilde-delimited crap and I can’t help but feel that there’s a more concise way to do this whole thing. But that’s not the point. I was able to think it and then do it. That’s why PHP wins.