• FragFrog (unregistered)

    And it keeps saddening me to see things like this:

    $type = $ainstall["$x[hostid].$x[hostname]"];

    Guess what you're doing? That's quite right: in your bright, shiny example you are letting PHP eval that string, the very same principle you're bashing the original writer for.

    A LOT of crap gets written in PHP because people don't know what they are doing, but to write truely good PHP code is both possible as well as just as hard as writing good code in any other language. It's just that PHP makes writing bad code so much easier :]

  • EatenByAGrue (unregistered)

    The real question is whether Jim's reporting application generated paper reports that could be faxed to the head office, placed on a wooden table, photographed, and the photograph emailed out.

  • EmperorOfCanada (unregistered)

    Maybe he had tonnes of Lisp/AI experience and was planning on building a self coding dynamic intelligent system? Or maybe he was a dumbass.

  • Nano (unregistered) in reply to FragFrog

    Fragfrog: eval != variable expansion

  • ZanderZ (unregistered) in reply to FragFrog

    QFT. Code like this gives me physical pain. Luckily I don't have a job as programmer so I don't have to maintain other peoples code :)

  • ZanderZ (unregistered) in reply to FragFrog
    FragFrog:
    And it keeps saddening me to see things like this:

    $type = $ainstall["$x[hostid].$x[hostname]"];

    Guess what you're doing? That's quite right: in your bright, shiny example you are letting PHP eval that string, the very same principle you're bashing the original writer for.

    A LOT of crap gets written in PHP because people don't know what they are doing, but to write truely good PHP code is both possible as well as just as hard as writing good code in any other language. It's just that PHP makes writing bad code so much easier :]

    QFT. Code like this gives me physical pain. Luckily I don't have a job as programmer so I don't have to maintain other peoples code :)

    (Damnit, forgot to quote the message in my previous post)

  • highphilosopher (unregistered)

    Unfortunately it's people like this that give PHP a bad name. There's nothing wrong with PHP, just something wrong with learning any language in 21 days.

  • Jim Lard (cs)

    Why does Jim need both "hostid" and "hostname" in his key anyway? Are there multiple servers with the same "hostid" but different "hostname"? I guess that's possible - if a machine is being dual-booted for multiple purposes for example. But are there also multiple servers with the same "hostname" but different "hostid"? That seems rather less likely.

  • noway! (unregistered) in reply to FragFrog
    FragFrog:
    And it keeps saddening me to see things like this:

    $type = $ainstall["$x[hostid].$x[hostname]"];

    Guess what you're doing? That's quite right: in your bright, shiny example you are letting PHP eval that string, the very same principle you're bashing the original writer for.

    That's variable expansion, not code evaluation. Although, i think the quotes are redundant... right?

  • Myname! (unregistered)

    The suggested code is a WTF in itself. If the value doesn't exist, then there's no need to set the result to "null" because it's already the type NULL which would turn into "null" at any string conversion. If the value was an empty string as the bad code suggested then isset() would return true and it'll stay as "".

  • Book title (unregistered)

    "Learn PHP in 21 hours"

    FTFY.

  • Myname! (unregistered) in reply to Myname!
    Myname!:
    The suggested code is a WTF in itself. If the value doesn't exist, then there's no need to set the result to "null" because it's already the type NULL which would turn into "null" at any string conversion. If the value was an empty string as the bad code suggested then isset() would return true and it'll stay as "".

    Actually, scratch that, just realised what the code was actually doing. Where's my coffee...

  • Schmalls (unregistered) in reply to noway!
    noway!:
    FragFrog:
    And it keeps saddening me to see things like this:

    $type = $ainstall["$x[hostid].$x[hostname]"];

    Guess what you're doing? That's quite right: in your bright, shiny example you are letting PHP eval that string, the very same principle you're bashing the original writer for.

    That's variable expansion, not code evaluation. Although, i think the quotes are redundant... right?

    No the quotes aren't redundant. It is doing the same thing as:

    $x[hostid] . '.' . $x[hostname]
    However, a big WTF is that the array key isn't quoted like so:
    $x['hostid'] . '.' . $x['hostname']
    When left unquoted, PHP first checks to see if there are constants named hostid and hostname and if there are none it emits a notice and uses the string instead.

  • The_Assimilator (unregistered)
    Jim aggressively fought off all attempts at transitioning the application the development team so, during a round of cost cutting, Jim was made redundant.

    PHP Parse error: parse error: supplied sentence does not contain valid grammar in C:\Inetpub\wwwroot\tdwtf\Articles\PHP-has-an-eval-function-like-perl.aspx on line 5

  • Anon Ymous (unregistered) in reply to The_Assimilator
    The_Assimilator:
    Jim aggressively fought off all attempts at transitioning the application the development team so, during a round of cost cutting, Jim was made redundant.

    PHP Parse error: parse error: supplied sentence does not contain valid grammar in C:\Inetpub\wwwroot\tdwtf\Articles\PHP-has-an-eval-function-like-perl.aspx on line 5

    There's nothing wrong with the grammar of that sentence. "Fight off" is a phrasal verb -- it has a different meaning than "fight". (Or are you whining about the use of the passive voice?)

  • Kermos (cs)

    Dunno if I can really say WTF here. The story itself clearly states this guy had absolutely no programming background and apparently, when he first wrote this application, I it doesn't appear there were any developers available to hand the task off to.

    Given that the developer team later on didn't completely scrap the entire code base and was able to refactor it to me indicates that overall, it couldn't have been that bad. I think primarily the project just grew to a size where this guy could no longer handle it with his limited experience.

    I think we all can agree that every one of us has produced WTF Worthy code when we first started writing software.

    Had this guy been someone claiming to be the greatest programmer of all time with a 6 digit salary, then yes, I'd definitely say WTF. But seeing how the story clearly states he was a beginner, I'd say rather than pointing at him and screaming WTF, someone should maybe consider giving a person like that some pointers in how to do things better. Never know, might actually get a great developer out of it eventually...

  • Robo (unregistered) in reply to Anon Ymous

    "transitioning the application the development team"?

  • Alex (unregistered) in reply to The_Assimilator

    Pedantic exception in PHP Parse error: parse error:

    Supplied sentence does contain valid grammar, it does however contain invalid grammar as well.

    Lol

  • Jim (unregistered)

    Now that I am redundant, I would just like to say that it's no fun being redundant.

  • MyKey_ (cs) in reply to Kermos
    Kermos:
    Dunno if I can really say WTF here. The story itself clearly states this guy had absolutely no programming background and apparently, when he first wrote this application, I it doesn't appear there were any developers available to hand the task off to.

    Given that the developer team later on didn't completely scrap the entire code base and was able to refactor it to me indicates that overall, it couldn't have been that bad. I think primarily the project just grew to a size where this guy could no longer handle it with his limited experience.

    I think we all can agree that every one of us has produced WTF Worthy code when we first started writing software.

    Had this guy been someone claiming to be the greatest programmer of all time with a 6 digit salary, then yes, I'd definitely say WTF. But seeing how the story clearly states he was a beginner, I'd say rather than pointing at him and screaming WTF, someone should maybe consider giving a person like that some pointers in how to do things better. Never know, might actually get a great developer out of it eventually...

    Agreed! But giving pointers to a newbie is a very, very bad idea. Hints are better!

    [UPDATE: Sorry, I forgot the mandatory http://xkcd.com/138/.]

  • mbvlist (cs) in reply to Schmalls
    Schmalls:

    No the quotes aren't redundant. It is doing the same thing as:

    $x[hostid] . '.' . $x[hostname]
    However, a big WTF is that the array key isn't quoted like so:
    $x['hostid'] . '.' . $x['hostname']
    When left unquoted, PHP first checks to see if there are constants named hostid and hostname and if there are none it emits a notice and uses the string instead.
    It doesn't work like that when in a string AFAIK. You can test it using this code: $array['true'] = 'The Real WTF is PHP syntax.'; $var1 = "$array[true]"; $var2 = $array['true'];

    I think $var1 and $var2 have the same value. Don't ask me why, it is pretty much crap anyway.

  • sao (unregistered) in reply to MyKey_
    MyKey_:
    Kermos:
    Dunno if I can really say WTF here. The story itself clearly states this guy had absolutely no programming background and apparently, when he first wrote this application, I it doesn't appear there were any developers available to hand the task off to.

    Given that the developer team later on didn't completely scrap the entire code base and was able to refactor it to me indicates that overall, it couldn't have been that bad. I think primarily the project just grew to a size where this guy could no longer handle it with his limited experience.

    I think we all can agree that every one of us has produced WTF Worthy code when we first started writing software.

    Had this guy been someone claiming to be the greatest programmer of all time with a 6 digit salary, then yes, I'd definitely say WTF. But seeing how the story clearly states he was a beginner, I'd say rather than pointing at him and screaming WTF, someone should maybe consider giving a person like that some pointers in how to do things better. Never know, might actually get a great developer out of it eventually...

    Agreed! But giving pointers to a newbie is a very, very bad idea. Hints are better!

    [UPDATE: Sorry, I forgot the mandatory http://xkcd.com/138/.]

    agreed, but only initialized pointers

  • Anonymous (unregistered)

    "Personal Home Page". That is all.

  • Severity One (cs) in reply to FragFrog
    FragFrog:
    ...but to write truely good PHP code is both possible as well as just as hard as writing good code in any other language.
    I would say it's a lot harder. :)
  • anonymous (unregistered) in reply to Anon Ymous
    Anon Ymous:
    The_Assimilator:
    Jim aggressively fought off all attempts at transitioning the application the development team so, during a round of cost cutting, Jim was made redundant.

    PHP Parse error: parse error: supplied sentence does not contain valid grammar in C:\Inetpub\wwwroot\tdwtf\Articles\PHP-has-an-eval-function-like-perl.aspx on line 5

    There's nothing wrong with the grammar of that sentence. "Fight off" is a phrasal verb -- it has a different meaning than "fight". (Or are you whining about the use of the passive voice?)

    The problem is that it should be "at transitioning the application TO the development team".

  • Anonymous (unregistered) in reply to MyKey_
    MyKey_:
    [UPDATE: Sorry, I forgot the mandatory http://xkcd.com/138/.]
    XKCD currently has the best Geocities commemoration I've ever seen - complete with blink tags, broken images and malformed HTML aplenty. It looks exactly like any Geocities page of the nineties. Good times.
  • Lee K-T (unregistered)
    FragFrog:
    A LOT of crap gets written in PHP because people don't know what they are doing...
    <flamewar> Well if they knew, they wouldn't be writing in PHP!!! :) </flamewar>
  • Schmalls (unregistered) in reply to mbvlist
    mbvlist:
    It doesn't work like that when in a string AFAIK. You can test it using this code: $array['true'] = 'The Real WTF is PHP syntax.'; $var1 = "$array[true]"; $var2 = $array['true'];

    I think $var1 and $var2 have the same value. Don't ask me why, it is pretty much crap anyway.

    You are correct. It doesn't matter since the argument does hold for Jim's code:
    $hostid = $aservers[$i][hostid];
    $hostname = $aservers[$i][hostname];

  • Airhead (cs) in reply to Myname!
    Myname!:
    The suggested code is a WTF in itself. If the value doesn't exist, then there's no need to set the result to "null" because it's already the type NULL which would turn into "null" at any string conversion. If the value was an empty string as the bad code suggested then isset() would return true and it'll stay as "".

    Setting variables is A Good Thing. If you need to check for that variable later, you don't allways have to check it with

    <?php if="" (isset($variable)="" &&="" $variable="" !="null)" ....=""?>

    Some of us use debugging with php.

  • Airhead (cs) in reply to Severity One
    Severity One:
    FragFrog:
    ...but to write truely good PHP code is both possible as well as just as hard as writing good code in any other language.
    I would say it's a lot harder. :)
    So, you don't know PHP? ;)
  • Martin (unregistered) in reply to EatenByAGrue
    EatenByAGrue:
    The real question is whether Jim's reporting application generated paper reports that could be faxed to the head office, placed on a wooden table, photographed, and the photograph emailed out.

    How can you please email a photograph? The photograph needs to be developed and scanned before emailing.

    Stick to the process in future!

  • Beaker (unregistered) in reply to EatenByAGrue
    EatenByAGrue:
    The real question is whether Jim's reporting application generated paper reports that could be faxed to the head office, placed on a wooden table, photographed, and the photograph emailed out.

    Sadly, that feature is not implemented yet. There are links on the page, and the managers are afraid that they might accidentally click on them. That would be very confusing for the user, so they need to find a way to disable any links before they can fax reports.

  • justsomedude (unregistered) in reply to Anonymous
    Anonymous:
    MyKey_:
    [UPDATE: Sorry, I forgot the mandatory http://xkcd.com/138/.]
    XKCD currently has the best Geocities commemoration I've ever seen - complete with blink tags, broken images and malformed HTML aplenty. It looks exactly like any Geocities page of the nineties. Good times.
    94 Errors, 22 warning(s) Oh Baby! LOL @ <html web="2.0"> and the GOTO 10 tag. </html>
  • Anthony (unregistered) in reply to mbvlist
    mbvlist:
    Schmalls:

    No the quotes aren't redundant. It is doing the same thing as:

    $x[hostid] . '.' . $x[hostname]
    However, a big WTF is that the array key isn't quoted like so:
    $x['hostid'] . '.' . $x['hostname']
    When left unquoted, PHP first checks to see if there are constants named hostid and hostname and if there are none it emits a notice and uses the string instead.
    It doesn't work like that when in a string AFAIK. You can test it using this code: $array['true'] = 'The Real WTF is PHP syntax.'; $var1 = "$array[true]"; $var2 = $array['true'];

    I think $var1 and $var2 have the same value. Don't ask me why, it is pretty much crap anyway.

    Correct, because within quotes it will never evaluate it to a constant (unless using the constant() function).

    What Schmalls was getting at is this

    $array['true'] = 'The Real WTF is PHP syntax.'; $var1 = "$array[true]"; $var2 = $array['true']; $var3 = $array[true];

    $var1 === $var2 != $var3

    Now, there is ambiguity in the quoted variable syntax. That's why I try to avoid it completely. But if I do it, I'll use the curly brace syntax to force the variable. So instead of "$array[true]", it would be "{$array['true']}".. That allows you to also do things like "{$word}[something]" without it trying to parse it as an array...

  • Jay (unregistered) in reply to Kermos
    Kermos:
    Dunno if I can really say WTF here. The story itself clearly states this guy had absolutely no programming background and apparently, when he first wrote this application, I it doesn't appear there were any developers available to hand the task off to.

    Given that the developer team later on didn't completely scrap the entire code base and was able to refactor it to me indicates that overall, it couldn't have been that bad. I think primarily the project just grew to a size where this guy could no longer handle it with his limited experience.

    I think we all can agree that every one of us has produced WTF Worthy code when we first started writing software.

    Had this guy been someone claiming to be the greatest programmer of all time with a 6 digit salary, then yes, I'd definitely say WTF. But seeing how the story clearly states he was a beginner, I'd say rather than pointing at him and screaming WTF, someone should maybe consider giving a person like that some pointers in how to do things better. Never know, might actually get a great developer out of it eventually...

    If an self-trained person on his first attempt at programming writes code that is so bad that it's funny, it is unfair to ridicule him. How do you expect him to learn until he gains some experience?

    But the code is still funny.

    And just because it's unfair to ridicule someone doesn't mean that we don't enjoy doing it.

  • Jay (unregistered) in reply to Beaker
    Beaker:
    EatenByAGrue:
    The real question is whether Jim's reporting application generated paper reports that could be faxed to the head office, placed on a wooden table, photographed, and the photograph emailed out.

    Sadly, that feature is not implemented yet. There are links on the page, and the managers are afraid that they might accidentally click on them. That would be very confusing for the user, so they need to find a way to disable any links before they can fax reports.

    I wouldn't be surprised if in a couple of decades we don't have paper thin computers that you CAN handle like a sheet of paper and poke your finger on a link to bring up the referenced page. And then people will read old jokes about links on print-outs and scratch their heads saying, "I don't get it. So the print-out had links in the text. What's the joke?"

    I saw a comedy that was made in the 60s where a guy is trying to bluff his way through a job interview. He's asked if he has any experience working with computers, and he says yes. Then he's asked where he gained this experience, and he says "My aunt had a computer." In the 60s this must have seemed absurd, like claiming that your aunt had a nuclear reactor in her tea room. But of course today, I'm sure many people have aunts who have computers at home.

  • Anonymous (unregistered) in reply to justsomedude
    justsomedude:
    Anonymous:
    MyKey_:
    [UPDATE: Sorry, I forgot the mandatory http://xkcd.com/138/.]
    XKCD currently has the best Geocities commemoration I've ever seen - complete with blink tags, broken images and malformed HTML aplenty. It looks exactly like any Geocities page of the nineties. Good times.
    94 Errors, 22 warning(s) Oh Baby! LOL @ <html web="2.0"> and the GOTO 10 tag. </html>
    My favourite:
    <script language="QBASIC">IF $BROWSER = "IE" THEN GOTO 50</script>
    
    ...also the single <java> tag that appears for absolutely no reason. </java>
  • BentFranklin (cs) in reply to Jay
    Jay:
    I saw a comedy that was made in the 60s where a guy is trying to bluff his way through a job interview. He's asked if he has any experience working with computers, and he says yes. Then he's asked where he gained this experience, and he says "My aunt had a computer." In the 60s this must have seemed absurd, like claiming that your aunt had a nuclear reactor in her tea room. But of course today, I'm sure many people have aunts who have computers at home.

    Unless your aunt was Grace Hopper.

  • Procedural (unregistered) in reply to Jim
    Jim:
    Now that I am redundant, I would just like to say that it's no fun being redundant.

    That's completely true.

  • evilspoons (cs)

    I'm surprised no one's mentioned the irony of Jim writing the system that was used to replace Jim.

  • Rocketeer (unregistered)

    I enjoyed reading that code - it was like a horror story for programmers :-)

       $hostid    = $aservers[$i][hostid];
       $hostname  = $aservers[$i][hostname];
       $systemkey = $hostid . "." . $hostname;
    

    It started off with a little light foreboding - just as we all know "It was a dark and stormy night..." is going to lead to trouble, so does gratuitous variable creation.

      for ($j=0; $j < count($akeys);="" $j++)="" {="" if="" ($systemkey="=" $akeys[$j])="" {="" $ainstall_values[$i]="$ainstall[$systemkey];" }="" }="">

    Then, as the story progresses, we start to feel fear at the unnecessary inner loop... okay, so far, it's a touch amateurish, but that's to be expected - yes, there's an easier way to do all this, but it's not actually bad per-se...

     // PHP has an eval function like perl.
     // Building up a switch statement  to run later.
     $switch  = "switch (\$ainstall[\$systemkey]) {";
     $avalues[count($aunique)];
    

    Then, like a scream cutting through the night... Eval? Nominative determinism raises it's head - just as Jack the Ripper is unlikely to be planning to convert your CDs to MP3s, a function named Eval is unlikely to be planning on anything good...

    To be honest, at this point I went and hid behind the sofa... I heard some more screaming, and a mechanical voice declaiming "E-val-u-ate... E-val-u-ate..." but I didn't dare look at the screen again until the end credits started rolling!

  • nube (unregistered)

    Why is this sort of thing a web application ? A system type person should know better.

  • EngleBart (unregistered) in reply to highphilosopher
    highphilosopher:
    Unfortunately it's people like this that give PHP a bad name. There's nothing wrong with PHP, just something wrong with learning any language in 21 days.
    I think the problem is that he learned PHP and how to program in 21 days!

    A programmer with a solid foundation in a single language should be able to produce workable code (not necessarily pretty) in a new language. (If there is a major paradigm shift, e.g. procedural to functional or procedural to tree(XSLT), add a few months for the brain to re-plasticize)

  • ZP (unregistered) in reply to Anon Ymous
    Anon Ymous:
    The_Assimilator:
    Jim aggressively fought off all attempts at transitioning the application the development team so, during a round of cost cutting, Jim was made redundant.

    PHP Parse error: parse error: supplied sentence does not contain valid grammar in C:\Inetpub\wwwroot\tdwtf\Articles\PHP-has-an-eval-function-like-perl.aspx on line 5

    There's nothing wrong with the grammar of that sentence. "Fight off" is a phrasal verb -- it has a different meaning than "fight". (Or are you whining about the use of the passive voice?)

    You completely missed the whole.

  • Dan (unregistered)
    Since it would have been wasteful to rewrite it from scratch, there are still vestiges of Jim in there

    In my experience, it's sometimes better to learn from the first attempt, then scrap it entirely and rewrite it. You often come up with better organization and improve maintainability to the nth degree that way. I've been involved in too many projects where "we don't have time to really fix it, so just fix the problem" and the "just fix it" only made the code harder to work with and often introduced even more problems.

  • dtaylor84 (cs) in reply to FragFrog

    Actually, no. In that example PHP is interpolating the string, not evaluating the string. Both can be problematic, at times, but interpolating input is a whole lot less likely to get you in trouble than evaluating input.

  • Procedural (unregistered) in reply to EngleBart
    EngleBart:
    highphilosopher:
    Unfortunately it's people like this that give PHP a bad name. There's nothing wrong with PHP, just something wrong with learning any language in 21 days.
    I think the problem is that he learned PHP and how to program in 21 days!

    A programmer with a solid foundation in a single language should be able to produce workable code (not necessarily pretty) in a new language. (If there is a major paradigm shift, e.g. procedural to functional or procedural to tree(XSLT), add a few months for the brain to re-plasticize)

    I hear they may possibly hypothetically be writing a follow-up to "Learn PHP in 21 Days". It's be called "Unlearn Your Hack Habits in 2.5 Years".

    It may not sell as much, but the market is huge.

  • Ralph (unregistered) in reply to Anthony
    Anthony:

    Correct, because within quotes it will never evaluate it to a constant (unless using the constant() function).

    What Schmalls was getting at is this

    $array['true'] = 'The Real WTF is PHP syntax.'; $var1 = "$array[true]"; $var2 = $array['true']; $var3 = $array[true];

    $var1 === $var2 != $var3

    Now, there is ambiguity in the quoted variable syntax. That's why I try to avoid it completely. But if I do it, I'll use the curly brace syntax to force the variable. So instead of "$array[true]", it would be "{$array['true']}".. That allows you to also do things like "{$word}[something]" without it trying to parse it as an array...

    Using true here is an array key is a bad example, because its also reserved for boolean truth, but the PHP wtf is that:

    $array['key'] = 'value';

    $var1 = $array['key']; $var2 = $array[key]

    Now $var1 === $var2, but you will get a warning on the line assigning $var2 about using constants as an array key, but the interpreter will guess your intention and use the string if it finds it. === is an operator that checks both type and value, so:

    1 == "1" //true 1 === "1" //false

    More WTF,

    $int = 1; $string1 = "the value of int is $int"; $string2 = 'the value of int is $int';

    $string1 will contain "the value of int is 1", while $string2 is "the value of int is $int" , single quotes are dumb strings, but when double quotes are used the interpreter will look at your string and insert any variables it finds.

  • JRiddy (cs) in reply to BentFranklin
    BentFranklin:
    Unless your aunt was Grace Hopper.

    Nice reference.

  • Shishire (unregistered) in reply to mbvlist
    <?php define('foo',="" 'bar');="" $var[foo]='baz' ;="" $var['foo']='random' ;="" echo="" "\$var[foo]="$var[foo]\n";" echo="" "\$var[bar]="$var[bar]\n";" echo="" '$var[\'foo\']='.$var[' foo']."\n";="" echo="" '$var[foo]='.$var[foo]."\n";
    echo ' $var[\'bar\']='.$var[' bar']."\n";=""?>
    
    Output:
    
    $var[foo] = random
    $var[bar] = baz
    $var['foo'] = random
    $var[foo] = baz
    $var['bar'] = baz
    

    The point here being that single and double quotes do different things, constants are resolved first, and that array keys inside of single quotes don't need to be re-quoted.

    That being said, imho, it's never a good idea to mix variables into static strings, which is why I always use single quotes, and break out whenever I need to insert a variable. Much safer, and no messy escape syntax to deal with (with the exception of ' and \ ).

    Captcha: odio Odious much?

Leave a comment on “"PHP has an eval function like perl"”

Log In or post as a guest

Replying to comment #:

« Return to Article