Home | Photography | Flickr | LiveJournal | Get Firefox

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:

$nav = $_SERVER["REQUEST_URI"];
$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:

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

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

switch(sizeof($vars))
{
case 2:
$team = $vars[1];
break;
case 3:
$team = $vars[1];
$player = $vars[2];
break;
}

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.

Share and Enjoy:
  • Facebook
  • StumbleUpon
  • Digg
  • e-mail
  • del.icio.us
  • Google
  • Reddit
  • Technorati
  • BlinkList
  • blogmarks
  • Blue Dot
  • description
  • Furl
  • Ma.gnolia
  • MisterWong
  • Netvouz
  • PlugIM
  • Propeller
  • Simpy
  • Spurl
  • TailRank