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

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Extra WTF for using argv/argc as local variables.

Heh sprintf probably means the person had a heavy c background.

Wow. Its an honour to be receiving this award. Thank you very much! :)

Some explanations are due.

I've been in c(++) for about 3 years before, and this is acctually my first attempt at php code, written pretty much just about 5 years ago. A week before i wrote this I first downloaded php. Needless to say, to me it was just a tricked out c/perl mix, so i could do the stuff the way i was used to.. the c(++) way.

(I got into perl later down the line finding it similar to php, i guess i could give you some wtfs from that era also ;))

And believe you me.. if i had known of $_GET/$_POST then, and implode/explode/array ... this code most probably wouldnt exist!!! ;)


Basically the following stuff can be explained in a c(++) way.


1. The function only returns a NULL!

With c(++), i had to put empty returns into functions, I think because of some compiler warnings (ie, function return doesnt dunno dunno datatype dunno dunno)

2. It uses references to return values

Lack of documentation in my head - the c(++) way of returning stuff (since i didnt know this had arrays :))...

3. It pretty pointless, especially when you have $_GET

Or auto_globals (which i realised about a week later).

4. It's doesn't really work

Sure it does! Or it did. Then.

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.

Maybe that behaviour has changed, but i assure you, even trough apache you have more than one element in the argv/argc variables... if you fill them. Ofcourse 5 years later it's completely beyond comprehension why anyone would use them, except for shell scripts...

6. $seperator[0] - WTF?! oh why!?

New to me, but i considered $seperator[0] a character, while $seperator would be a string (but its all string, didnt know then).

Oh Uh.

nope, sorry. i've coded in c++. this is still ridiculous.

why would you feel the need to unset $str, but not $seperator?

why would you concatenate one way the first time and a completely different way the second time?

how could you have learned of the existance of rawurldecode() yet not known that it takes string input? or if you knew it took string input, why were you sending what you thought was a single character?

this code never could have worked in anything that's ever been called "php" by anyone but you.

sigh. okay so i was sporadic about unset (kinda felt it was like free() in c).. memory leaks happen (before you learn there is something called a garbage collector)..

used sprintf because i didnt know the '.' as i said. you see that i add a space between the string.. and well.. again *first php code ever* ;)...

dunno about rawurldecode, but i know i had probs with specialchars (ie %20 and stuff in the url), so i need a function.. this one came up in the manual...

where you got the idea i thought i was passing single characters here is beyond me. I just thought when comparing that it depended on some kind of data type also.

but i swear.. this code was valid and working in 4.0.6, and im quite sure it would work on 4.latest without many if any modifications. In fact, just to prove it to you.. i pasted this into a file and loaded it trough apache with the following url:

wtf.php?blog=1&title=title&more=1&c=1&tb=1&pb=1

$my_argc = 0;
$my_argv = array();

split_arguments($_SERVER['argv'],$_SERVER['argc'],&$my_argc,&$my_argv);

var_dump($my_argc);
var_dump($my_argv);


int(5)
array(6) {
[0]=>
string(6) "blog=1"
[1]=>
string(11) "title=title"
[2]=>
string(6) "more=1"
[3]=>
string(3) "c=1"
[4]=>
string(4) "tb=1"
[5]=>
string(4) "pb=1"
}

Needless to say, the next three functions also created itself just after this one.

/* returns boolean value of a variable (checkbox type) */

function get_boolean($name,$nr_args,$args)
{
for ($i=0; $i

ergh, shitty character limit :/... well.. i guess its daily wtf material for a later time.

We all make mistakes when we first start out, that's my one complaint about the WTF sites. You should have seen some of my early doozies, lol.

All in all, if you had done that today i wouldn't post being sympathetic. However, when just starting out, got to give some break. I remember trying to get the current URL for page handling. Coded it and took like 8 lines, using globals and unsafe things. Recoded it later, did it in 3 lines or so.

This thing's comment parser barfs every time it sees less than angle bracket, actually. Annoying.

The $separator[0] isn't terrible, it took me some time figure out that in PHP it's $separator{0}.

phrax, $argv isn't a PHP predefined variable, it's just another random local variable with a misleading name. (Good wtf there.) I use $_SERVER['argv'] all the time, though, because some of my scripts are meant to be used in both a web/web services environment and a shell scripting one (esp cron scripts). Naturally I normalize all that away in a global header first.

p.s. $seperator{0} was acctually introduced later down the line, around 4.2 (dont quote me on this).. but generally $seperator[0] was the supported string indexing method before the introduction of {}. [] is still supported for backwards compatibility in 4.x, it might have been dropped in php5, but i think not

anyway, i bet you have similar wtfs from your first days :)

I'd prefer to believe that I was immaculately concieved and born as an intermediate programmer, and to pretend that the folder of two-year-old PHP (to say nothing of the much older C folder and positively ancient BASICA folder) were written by someone I don't know and wouldn't care to meet. ;) I'm sure they'd make great wtfs if I wasn't too frightened to open them.

Also, I didn't know it was a recent thing. Makes more sense then.

Well, full respect to the ori

Well, full respect to the original author for being brave enough to come forward :) I don't know if I would be brave enough, when somebody posts some of my first code on here!

Post new comment




*

  • Web and e-mail addresses are automatically converted into links.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <pre> <p> <br /> <br>