December 16th, 2002:

everyone’s an expert

I love PHP. I really do. I swear. I just really get tired of seeing so many “articles” and “tutorials” and “code examples” that are complete and utter shit written by people who claim to be “experts”. Maybe I’m being a bit harsh. Let me give you an example.

Take an article written by Joe O’Donnell entitled “Using ForceType for Nicer Page URLs“. It’s a great idea. It’s something I wish every PHP application author would do. Nothing, to me, is uglier than a URL with a bunch of question marks, ampersands, and equal signs. This article informs all of its readers of the virtues of ForceType and the benefits of nice clean URLs. That’s great.

However, the second the author gets into the real PHP portion of the article, things turn sour.

This REQUEST_URI variable is the key to implementing the ForceType directive in combination with a PHP script.

Those of you who aren’t PHP programmers probably don’t see anything wrong with such a simple, direct statement. And that’s exactly my point. The novice, and perhaps even slightly advanced, PHP programmer might not realize there is anything wrong with that statement either. But there is.

The author then shows some code to use this “REQUEST_URI” variable in PHP:

$script = $_SERVER["SCRIPT_NAME"];
$nav = ereg_replace("^$script", "", $nav); 

Here’s the problem: The author of this article is performing two variable assignments and an expensive regular expression operation to end up with a string of text that is provided by PHP already. Instead of using REQUEST_URI one should merely use PATH_INFO. PATH_INFO is already equal to the information this author performed three separate operations to obtain. Of course, it doesn’t surprise me to see this author using double quotes, when the “less expensive by a factor of two” single-quote would do just fine. I could continue picking apart his article, but it wouldn’t do any good.

I don’t mean to come off rude. My code isn’t perfect. Far from it. But when I publish a tutorial, offer code samples, or submit an idea for mass consumption by PHP programmers new and seasoned alike, I at least make sure I’m writing about something I understand. And, even then, I send what I’ve written to a fellow programmer or two (or three) to look over and make sure there aren’t any glaring mistakes.

If any of you are looking for the proper code to use in this author’s example, you merely need to replace this chunk of his code:

$script = $_SERVER["SCRIPT_NAME"];
$nav = ereg_replace("^$script", "", $nav);

$vars = explode("/", $nav);
$team = "";
$player = ""; 

case 2:
$team = $vars[1];
case 3:
$team = $vars[1];
$player = $vars[2];

With this:

list($empty,$team,$player) = explode('/',$_SERVER['PATH_INFO']);

Sigh. Maybe I should start a “Stupid PHP code” site offering the best and worst moments of stupid PHP code from around the net.

Maverick -> Ambivalence

For you Java folks, Maverick looks like an interesting project. It’s another web application framework pushing the MVC (Model 2) pattern similar to Struts. [via Matt Croydon]

More interesting than that — to me anyway — was a link I found on the Maverick site to “PHP Maverick” or Ambivalence. I haven’t dug very deep into this yet, however, at first glance, my impressions of it are better than they were with Phrame (which is modeled after Struts) for several reasons.

First of all, it uses an XML based mappings file instead of a native PHP file. Editing such a complex structure using native PHP code just looks ugly. Secondly, instead of using $_REQUEST variables (via GET or POST) to determine which “action” it should take, it uses $_SERVER['PATH_INFO'] — a much cleaner approach in my opinion.

I’ve been working on a mini-tutorial for using Phrame. However, if this framework appears to operate cleaner, I’ll rewrite it to use Ambivalence instead. The best part about that is, either way, I’m using the MVC pattern, so almost all of my code merely plugs into an entirely new framework. Can I get three cheers for MVC?

machine-readable copyright licenses

Creative Commons has launched it’s first product today: Machine-Readable Copyright Licenses. They support many different licenses and have a easy to use interface for deciding which license is right for you. [via BurningBird and Matt Croydon]