Skip navigation.
Home

Wonky Code

More solutions for idle CPU time.

Wonky Code

Yesterday we had a great example of how to make sure your database isn't slacking off. Here's a example to make sure your web server keeps working hard too.

Also thanks to everybody who submitted a WTF. Keep them coming! :)

<?php
if( $_USER['uid'] > 1 )
    {
        
$usermenu = new Template( $_CONF['path_layout'] );
        
$usermenu->set_file( array( 'option' => 'useroption.thtml',
                                    
'current' => 'useroption_off.thtml' ));
        
$usermenu->set_var( 'site_url', $_CONF['site_url'] );
        
$usermenu->set_var( 'layout_url', $_CONF['layout_url'] );
        <
strong>$usermenu->set_var( 'block_name', str_replace( '_', '-', 'user_block' ));</strong>

        if( empty(
$title ))
        {
            
$title = DB_getItem( $_TABLES['blocks'], 'title', "name='user_block'" );
        }
    
//....
?>

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]);
}
?>
XML feed