Skip navigation.
Home

A function made up of WTFs...

Wonky Code

Props to Alex @ thedailywtf.com for today's PHP WTF.

I'm rating this as a WTF because:

  1. The function only returns a NULL!
  2. It uses references to return values
  3. It pretty pointless, especially when you have $_GET
  4. It's doesn't really work
  5. $argv will always have just one element, unless you're running PHP as a shell script interpreter. Then maybe there would be more than 1 argument.
  6. $seperator[0] - WTF?! oh why!?
  7. It's just soooooo bad. This function is basically made up of different WTF's.

I hope this isn't used in production somewhere... <?php
/* joins argv into one string and then splits it into logical
elements (html formated)*/

function split_arguments($argv,$argc,&amp;$nr_arg,&amp;$args)
{
       
$str="";
       
$seperator = "&";
       for (
$i=0; $i&lt;$argc; $i++)
       {
               if (
$i==0) {
                       
$str = sprintf("%s",$argv[$i]);
               } else {
                       
$str = sprintf("%s %s",$str,$argv[$i]);
               }
       }
       
trim($str);
       for (
$i=0; $i&lt;strlen($str); $i++)
       {
               if (
$str[$i] == $seperator[0])
               {
                       
$nr_arg++;
               } else {
                       
$args[$nr_arg] .= $str[$i];
               }
       }
       unset(
$str);
       for (
$i=0; $i&lt;=$nr_arg; $i++)
       {
               
$args[$i] = rawurldecode($args[$i]);
       }
       return;
}
?>

I particularly love the way $str is concatenated! I've never seen it done like this before:

<?php
if ($i==0) {
        
$str = sprintf("%s",$argv[$i]);
} else {
        
$str = sprintf("%s %s",$str,$argv[$i]);
}
?>

Yea.. nice example Chris, maybe next you'll be right!

Bad Architecture

There's this nagging voice in the back of my head that says, "don't make fun of the readers! They anger easy and won't come back". However, I couldn't resist reponding to Chris's post about using arrays to create strings because it's faster than concatenation!

So this is today's WTF:

You do realize that cating strings is slower than manipulating arrays, right?
<?php
// meh...
/* -->8-- */
// update the area code
$stSQL .= "$areacode=$codeData, ";

// update the phone num
$stSQL .= "$phonenum='$phoneData' ,";
/* -->8-- */

// yay!
/* -->8-- */
// update the area code
$update_array[] = "$areacode=$codeData";

// update the phone num
$update_array[] = "$phonenum='$phoneData'";

$query = "UPDATE tablename SET " . implode(', ', $update_array) ....;
/* -->8-- */
?>

And of course, you should always be checking to see if a variable actually exists, rather than simply assuming it will be there (*cough*$_POST[$areacode]*cough*). I hope this is just an oversight while making an example of poor code.

But from this little shell script we find that string concatenation is more than twice as fast than using arrays.

Run Number #1
1199999 - bigString, Time: 0.5843870639801
1199999 - bigString2, Time: 1.0092749595642
Run Number #2
1199999 - bigString, Time: 0.34683203697205
1199999 - bigString2, Time: 1.0810549259186
Run Number #3
1199999 - bigString, Time: 0.35090112686157
1199999 - bigString2, Time: 1.0080449581146
...

Well if you look at the shell script code you'll see I do 100,000 iterations. But if you only do like 20 iterations using an array is actually faster, take a look:

239 - bigString, Time: 0.0015630722045898
239 - bigString2, Time: 0.0010149478912354

Only after about 150 iterations does string concatenation become faster than using an array. See:

1799 - bigString, Time: 0.0024600028991699
1799 - bigString2, Time: 0.0025348663330078

So yea he's right, but then again, who the hell cares about those thousandths of a second? At least I'll be right when generating SQL for tables with more than a few hundred columns! ;b

/petty.