• thetiredsaint (unregistered)

    This same thing works in Python.

    for cat in cat:
      …
    

    Does exactly the same as the above PHP code.

  • yetihehe (unregistered) in reply to henke37
    henke37:
    Foreach isn't quite as simple one would think in php, try explaining this one: http://pastebin.com/m4fmWCPD
    You did in this pastebin's title. Foreach copies an array and iterates over this copy. So if you change array inside loop, it still iterates over old copy. This also explains why $cat still works, it's iterating over copy.
  • AGray (unregistered) in reply to MightyM
    MightyM:
    the beholder:
    And I always thought curiosity killed the $cat, when it actually was just pointing to another variable.

    So it's Schrödinger's $cat?

    If you seal a $cat in $address, with a vial of $liquid, how do you observe to see if $cat is still allocated?

    CAPTCHA: nisl - a slightly nicer needle. No, that is not the answer to Schrodinger's $cat.

  • Nagesh (unregistered)

    Also in Indian $cow is sacred $cat aint

  • (cs) in reply to Recursive Reclusive
    Recursive Reclusive:
    Scrummy:
    The less strongly-typed the language, the more important good unit testing is. This is a cornerstone of Agile development.
    Does that have any relevance to the story?

    Apparently you skipped over the story, and headed right to the comments section.

  • Doug (unregistered) in reply to ntroutman

    A particular use of the macro processor in C and C++. It often happens that adding a new feature requires a predictable sequence of changes over a number of files. It is tedious to visit each file to make the changes, and hard to remember all the changes needed. I make an include file that has one feature per line, with all the feature's parameters wrapped in a macro call. I always name the macro MC (probably short for MaCro). Then throughout the code when I'm defining some aspect of these features, I redefine the macro MC and #include the file. This means I only have to add one line to one file to implement the feature (or the predictable parts of it anyway), I have all the other features in front of me, so it's easy to spot discrepancies, and I can be sure I updated all the proper spots. Looks like a crock, though, and IDE's don't always like it.

    For example, here are some lines from OpsMC.h

    MC(ADD,+,Add),
    MC(SUB,-,Sub),
    MC(MUL,*,Mul),
    

    OpTypes.h includes it to make an enum of ADD_OP, SUB_OP, etc and NUM_OPS

    #ifdef MC
    #undef MC
    #endif
    #define MC(type, symbol, text) type##_OP
    typedef enum {
    #include "OpsMC.h"
    NUM_OPS
    } Op;
    

    In a .C file three arrays are set up, which can be indexed by the enum above and will stay in synch when someone adds another operator.

    // The classifierSymbol and classifierText arrays are static to ClassifierOperation
    #ifdef MC
    #undef MC
    #endif
    #define MC(type, symbol, text) #symbol
    std::string ClassifierOperation::classifierSymbol[] =
    {
    #include "OpsMC.h"
    };
    #undef MC
    #define MC(type, symbol, text) #text
    std::string ClassifierOperation::classifierText[] =
    {
    #include "OpsMC.h"
    };
    #undef MC
    #define MC(type, symbol, text) #type
    std::string ClassifierOperation::classifierTypeName[] =
    {
    #include "OpsMC.h"
    };
    

    The payoff is greater in more complex situations. Sorry for the slow response, it took time to boil this down.

  • sfs (unregistered) in reply to thetiredsaint
    thetiredsaint:
    This same thing works in Python.
    for cat in cat:
      …
    

    Does exactly the same as the above PHP code.

    Sure. There are a number of languages that allow you to play fast and loose with the scope of variable definitions. This doesn't mean that you should do it.

    The WTF wasn't "this is PHP." The WTF was "look what heinous thing someone did in PHP."

  • stew (unregistered)

    So a professional programmer, claiming proficiency in a specific language, is hired to completely overhaul a code base written in that language.

    Said professional, when confronted with a simple control structure, is then mystified by that language's basic behavior.

    I think I spotted the WTF.

  • Jazz (unregistered) in reply to Leo
    Leo:
    Jazz:
    In before everyone insists, without any rational reason or documentation, that PHP is a horrible mutant shitpile that deserves to be ridiculed, belittled, and exterminated.

    ...but it is. Anyone who likes PHP is by definition a terrible developer.

    And just as I predicted, you have no rational reason for that statement, or any documentation which suggests that it's true, even a little.

    It's like you're trying to prove me right.

  • Jazz (unregistered) in reply to foo
    foo:
    Jazz:
    In before everyone insists, without any rational reason or documentation, that PHP is a horrible mutant shitpile that deserves to be ridiculed, belittled, and exterminated.
    s/before/after/;s/without/with/ (see 382952 and read the essay it mentions)

    Charles is not "everyone." Four words in quote marks are not "documentation," or even a decent citation. And the essay, which I went and googled at your request, is a poorly-supported opinion piece written by a developer who either can't or won't take the time to understand a language before criticizing it.

    His metaphor of PHP as a toolbox is a good one, but when he grabs a tool and it's not what he needs, he immediately jumps to "OMG PHP's tool X doesn't do Y!" when only two more seconds of searching would reveal that if he pulled the Y tool out of the box, it would do exactly what he needs.

    Sure, plenty of crap developers make pentagonal houses which fall apart when you knock on them using PHP. Plenty of good developers make flexible, secure websites with PHP, too, but you don't hear about them, because you like having PHP as the designated scapegoat to rag on.

    Either show me some documentation that isn't one person's closed-minded uneducated opinion rant, or shut the fuck up.

  • (cs) in reply to Jazz
    Jazz:
    foo:
    Jazz:
    In before everyone insists, without any rational reason or documentation, that PHP is a horrible mutant shitpile that deserves to be ridiculed, belittled, and exterminated.
    s/before/after/;s/without/with/ (see 382952 and read the essay it mentions)

    Charles is not "everyone." Four words in quote marks are not "documentation," or even a decent citation. And the essay, which I went and googled at your request, is a poorly-supported opinion piece written by a developer who either can't or won't take the time to understand a language before criticizing it.

    His metaphor of PHP as a toolbox is a good one, but when he grabs a tool and it's not what he needs, he immediately jumps to "OMG PHP's tool X doesn't do Y!" when only two more seconds of searching would reveal that if he pulled the Y tool out of the box, it would do exactly what he needs.

    Sure, plenty of crap developers make pentagonal houses which fall apart when you knock on them using PHP. Plenty of good developers make flexible, secure websites with PHP, too, but you don't hear about them, because you like having PHP as the designated scapegoat to rag on.

    Either show me some documentation that isn't one person's closed-minded uneducated opinion rant, or shut the fuck up.

    The real WTF is computer fucking programmers. Fuck off and die the lot of you cunts.

  • Recursive Reclusive (unregistered) in reply to Scrummy
    Scrummy:
    Recursive Reclusive:
    Scrummy:
    The less strongly-typed the language, the more important good unit testing is. This is a cornerstone of Agile development.
    Does that have any relevance to the story?

    Apparently you skipped over the story, and headed right to the comments section.

    Nope. Now answer the question.

  • anony-mouse (unregistered) in reply to Jazz
    Jazz:
    foo:
    Jazz:
    In before everyone insists, without any rational reason or documentation, that PHP is a horrible mutant shitpile that deserves to be ridiculed, belittled, and exterminated.
    s/before/after/;s/without/with/ (see 382952 and read the essay it mentions)
    Sure, plenty of crap developers make pentagonal houses which fall apart when you knock on them using PHP. Plenty of good developers make flexible, secure websites with PHP, too, but you don't hear about them, because you like having PHP as the designated scapegoat to rag on.

    Either show me some documentation that isn't one person's closed-minded uneducated opinion rant, or shut the fuck up.

    Yeah, a good carpenter can make a good mouse with a rock instead of a hammer, but why would you?

    He doesn't complain about PHP not being able to do X or Y, he complains about the inconsistencies with the language and it's irrational, unpredictable naming convention for things.

    I also find it funny you call him close minded when you're the one screaming how good PHP is without being able to even acknowledge its obvious flaws.

    Stay pist, PHP guy.

  • George (unregistered) in reply to Jay
    Jay:
    If it involves reading a file with fixed-length fields and printing a report, you may be able to do it with COBOL. But it's very difficult to hurt yourself. It's a child's plastic hammer language.
    When I wrote some COBOL code to do string manipulation (change LASTNAME, FIRSTNAME to Firstname Lastname) the old timers got the strangest looks on their faces. It was not merely that what I had done was impossible to do, it was even impossible to imagine.

    Of course that was way back then. Back before simply knowing COBOL meant I too, am an oldtimer! :(

  • David F. Skoll (unregistered) in reply to sfs

    Here's Perl:

    use Data::Dumper;
    my $cat = ['foo', 'bar', 'quux'];
    foreach $cat (@$cat) {
            print "$cat\n";
    }
    
    print Dumper($cat);
    

    And the results:

    $ perl cat.pl 
    foo
    bar
    quux
    $VAR1 = [
              'foo',
              'bar',
              'quux'
            ];
    

    Arguably more "correct"?

  • (cs)

    Not enough possible variable names.

    Must use same name for many variables.

  • Spewin Coffee (unregistered) in reply to PleegWat

    You are giving too much credit to the authors of the code. The correct response is:

    'svn' is not recognized as an internal or external command, operable program or batch file.

  • Jack (unregistered)

    Am I reading it wrong, or did the story imply that the for loops are hidden in include files?

    $cat = [1, 2, 3, 4, 5];
    #include("for_loop.php");
    echo $cat; // 5
  • (cs) in reply to Jazz
    Jazz:
    foo:
    Jazz:
    In before everyone insists, without any rational reason or documentation, that PHP is a horrible mutant shitpile that deserves to be ridiculed, belittled, and exterminated.
    s/before/after/;s/without/with/ (see 382952 and read the essay it mentions)

    Charles is not "everyone." Four words in quote marks are not "documentation," or even a decent citation. And the essay, which I went and googled at your request, is a poorly-supported opinion piece written by a developer who either can't or won't take the time to understand a language before criticizing it.

    His metaphor of PHP as a toolbox is a good one, but when he grabs a tool and it's not what he needs, he immediately jumps to "OMG PHP's tool X doesn't do Y!" when only two more seconds of searching would reveal that if he pulled the Y tool out of the box, it would do exactly what he needs.

    Sure, plenty of crap developers make pentagonal houses which fall apart when you knock on them using PHP. Plenty of good developers make flexible, secure websites with PHP, too, but you don't hear about them, because you like having PHP as the designated scapegoat to rag on.

    Either show me some documentation that isn't one person's closed-minded uneducated opinion rant, or shut the fuck up.

    I hate php and i have been working with it for years. I still work with it because its what several of my clients want. I agree with a lot of the opinions in the essay as a result of personal experience. Everyday, not most days, but every single day I work with it, it always does something brand new to fuck with me. So no, its not documentation, but when enough people develop an opinion towards something, facts become less important. Its just a shitty language. Thats my opinion. That is a lot of peoples opinions. Its not closed minded, because it is a result of experience.

  • Muzer (unregistered) in reply to Jack
    Jack:
    Am I reading it wrong, or did the story imply that the for loops are hidden in include files?
    $cat = [1, 2, 3, 4, 5];
    #include("for_loop.php");
    echo $cat; // 5

    Certainly sounded like it implied that to me.

  • M (unregistered)

    cat fridge Error: cat cannot open fridge.

    Cracks me up every time.

  • Larry (unregistered) in reply to M
    M:
    > cat fridge Error: cat cannot open fridge.

    Cracks me up every time.

    You almost got it right...

    $ cat 'door: no hands' cat: cannot open door: no hands

  • RoadieRich (unregistered)

    I want to hug every $cat, but I can't, because they've been overwritten.

  • (cs) in reply to Recursive Reclusive
    Recursive Reclusive:
    Scrummy:
    Recursive Reclusive:
    Scrummy:
    The less strongly-typed the language, the more important good unit testing is. This is a cornerstone of Agile development.
    Does that have any relevance to the story?

    Apparently you skipped over the story, and headed right to the comments section.

    Nope. Now answer the question.

    The point that you are apparently not capable of comprehending is that a language like PHP is notoriously vulnerable to mysterious errors, because there is nothing to prevent scenarios like we see IN THIS STORY in advance of encountering it during runtime. With good unit testing this would be caught before it turned into a search akin to finding a needle in a haystack. For this reason, I would also go as far as to contend that PHP is a poor choice for Agile development.

    Do you see the relevance now? Or are you such a hyper-defensive PHP developer that you can't handle a civil discussion about the pros and cons of your beloved platform?

  • Kriis (unregistered) in reply to Code Slave
    I suppose

    svn blame <file>

    would do nothing but removed plausible deniability as to why the previous programmer's body parts has been stapled to bulletin board in the coffee room.

    I hope I'm not the first to question your assumption that they have SVN that goes back that far...

  • blowhole (unregistered)

    This isn't a big deal and that's just scripting languages for you. It should be obvious that it works that way. You should know that a lot of things in scripting languages work when you wouldn't expect coming from the background of a stricter language. Never expect things, read the documentation and experiment to find out how your language works. Turn on strict error reporting as well.

    I have seen far worse abominations in PHP, for example someone grossly misusing $$ and ${} (yes, that potentially allowed variable injection) to have a variable where the name is a number and the funny thing at the time is that the code was so simple it didn't even need an array. Passing the number in a normal variable was enough. I wondered at the time if someone made a typo of $$var instead of $var and rather than just correcting the typo, they changed the logic of the code to handle it everywhere else. I couldn't understand how someone could have been able to handle something so complex as the solution they created yet not be able to see the more obvious concise solution. That WTF blows nearly every PHP WTF I've seen here out of the water and it is only one of many such WTFs I've seen out there.

    Far worse is foreach($arr as &$ele).

  • Brendan (unregistered)

    But what you don't know I'm gonna tell you right now That the stutter and the $cat is the same thing Yo, I'm the $cat man Where's the $cat man? I'm the $cat man.

  • Gibbon1 (unregistered) in reply to Scrummy
    Scrummy:
    The less strongly-typed the language, the more important good unit testing is. This is a cornerstone of Agile development.

    Some people say PHP developers don't know Agile. This is wrong, they are very agile. If you've every some them flee to the next job before the codebase assplodes up you know what I mean.

    (In COBOL they use the waterfall model, or did. I mean it worked fine when they started the project back in 75. If they ever start another project they may well use it again)

  • (cs) in reply to blowhole
    blowhole:
    This isn't a big deal and that's just scripting languages for you. It should be obvious that it works that way. You should know that a lot of things in scripting languages work when you wouldn't expect coming from the background of a stricter language. Never expect things, read the documentation and experiment to find out how your language works. Turn on strict error reporting as well.

    I have seen far worse abominations in PHP, for example someone grossly misusing $$ and ${} (yes, that potentially allowed variable injection) to have a variable where the name is a number and the funny thing at the time is that the code was so simple it didn't even need an array. Passing the number in a normal variable was enough. I wondered at the time if someone made a typo of $$var instead of $var and rather than just correcting the typo, they changed the logic of the code to handle it everywhere else. I couldn't understand how someone could have been able to handle something so complex as the solution they created yet not be able to see the more obvious concise solution. That WTF blows nearly every PHP WTF I've seen here out of the water and it is only one of many such WTFs I've seen out there.

    Far worse is foreach($arr as &$ele).

    Probably got confused by the word "code" in "source code" and thought it meant something that you'd need an Enigma machine to make head or tail of. (Apologies for the misappropriation of terms and concepts for the purpose of comic effect.)

  • timok (unregistered) in reply to dogmatic
    dogmatic:
    The one thing I can say for php, at least it isn't javascript. Now with html5 web devs are expected to make full scale web apps using js, a language that doesn't support file includes and object orientation only through a whole lot of syntactic sugar. And it has to run on many platforms with different implementations of js. Now QA and debugging time has multiplied by an order of 10.

    Next time you bash a language, maybe learn a bit about it first. JavaScript is prototype based. If you try to write Java-like object-oriented code in it, you are doing it wrong. That's not a problem of the language. A screwdriver is not a bad tool just because someone insists on using it with nails. And if your web developers don't know how to write tests for their JavaScript, find better web developers. The tools exist and when combined properly and sticking to standards (and well-tested frameworks), you can have as much confidence in your JavaScript as in your server-side code.

  • corroded (unregistered) in reply to Tobias Rohde
    Tobias Rohde:
    I expected that it loops only once and $cat is the first element of the array $cat.

    Maybe the code is prehistoric (PHP 3 and older). The function array_pop was implemented in PHP 4.

    You mean exactly like the article says?

  • M (unregistered) in reply to BR

    There's a difference between "likes" and "is prepared to use if paid well enough".

  • Dave (unregistered) in reply to Coyne
    Coyne:
    It's quite simple to understand, m'kay? $cat is in scope outside the loop, and $cat is a local variable in scope only within the loop.

    Got that?

    Nope. PHP does not have arbitrary block scoping.

  • foxyshadis (unregistered) in reply to Coyne
    Coyne:
    It's quite simple to understand, m'kay? $cat is in scope outside the loop, and $cat is a local variable in scope only within the loop.

    Got that?

    PHP doesn't work like you think it works, obviously. Scope is programmer-managed, not language-managed, which is a nice way of saying that there is no scope. (There is a completely separate global scope from function & class scope, but no block scopes. If you need that distinction, your code sucks.)

    However, the particular behavior mentioned in the WTF has been around and documented since the very beginning of the language. It's actually explicitly called out in the docs that when you manipulate anything in a foreach, the foreach continues to happily truck along as if nothing happened, whereas manipulating it in a for or while will immediately act upon your changes. It's a nuance of PHP but one that any PHP developer should be completely familiar with even if they never use it, because it's called out in a giant notice box of the farking foreach documentation that everyone has to read. Unless they're writing C in PHP, which is a WTF in itself.

    You can foreach your way out of a lot of bull if you love abusing code. I kind of lost my love of that quite a few years ago, though.

    All languages have cruft, some just revel in it more than others. Thankfully current PHP no longer even supports a lot of the long deprecated crap code, unfortunately that means most web hosts refuse to upgrade because their customers have so much crap code. Much like the Python 3 problem....

    /end rant

  • Dave (unregistered) in reply to Scrummy
    Scrummy:
    The point that you <meaningless gibberish snipped>

    You appear to be terminally hard-of-understanding. It has at no point been established that there is a bug in that code, or that it doesn't do exactly what the developer intended.

    How would your magic tests help here?

  • (cs) in reply to Brendan
    Brendan:
    But what you don't know I'm gonna tell you right now That the stutter and the $cat is the same thing Yo, I'm the $cat man Where's the $cat man? I'm the $cat man.
    You obviously misread. No scat man involved.

    Maybe this could suit you better ? [image]

  • Herr Otto Flick (unregistered) in reply to Scrummy
    Scrummy:
    The less strongly-typed the language, the more important good unit testing is. This is a cornerstone of Agile development.

    Unit testing only tests whether the code produces the correct results. It doesn't test whether the code is batshit insane, written by a 14 yr old who has been up for 3 days, or only working by happenstance.

    It is entirely plausible that code which passes unit tests is as bollocks as this code is.

  • mouse building pedant (unregistered) in reply to anony-mouse
    anony-mouse:
    Jazz:
    foo:
    Jazz:
    In before everyone insists, without any rational reason or documentation, that PHP is a horrible mutant shitpile that deserves to be ridiculed, belittled, and exterminated.
    s/before/after/;s/without/with/ (see 382952 and read the essay it mentions)
    Sure, plenty of crap developers make pentagonal houses which fall apart when you knock on them using PHP. Plenty of good developers make flexible, secure websites with PHP, too, but you don't hear about them, because you like having PHP as the designated scapegoat to rag on.

    Either show me some documentation that isn't one person's closed-minded uneducated opinion rant, or shut the fuck up.

    Yeah, a good carpenter can make a good mouse with a rock instead of a hammer, but why would you?

    He doesn't complain about PHP not being able to do X or Y, he complains about the inconsistencies with the language and it's irrational, unpredictable naming convention for things.

    I also find it funny you call him close minded when you're the one screaming how good PHP is without being able to even acknowledge its obvious flaws.

    Stay pist, PHP guy.

    this modern learning astounds me ... explain again how a carpenter (good or otherwise) can make a good mouse with either a rock or a hammer?

    also ... can he make bad mice as well?

    anyway, presumably the $cat will be happy either way

  • Spudley (unregistered)

    Everytime you iterate foreach($cat as $cat) a kitten dies.

  • (cs) in reply to Spudley
    Spudley:
    Everytime you iterate foreach($cat as $cat) a kitten dies.
    Actually only a kitten survives. The rest die.
  • Ritesh (unregistered)

    You let the cat out of the bag!

  • Arclight (unregistered) in reply to BR
    BR:
    Leo:
    ...but it is. Anyone who likes PHP is by definition a terrible developer.

    So half of the front-end developers at Facebook then?

    Well you are almost there. Not being a PHP developer doesnt infer that you are not a terrible developer. So your statement should have read "At least half ...".

  • M (unregistered) in reply to Jazz
    Jazz:
    And the essay, which I went and googled at your request, is a poorly-supported opinion piece written by a developer who either can't or won't take the time to understand a language before criticizing it.

    His metaphor of PHP as a toolbox is a good one, but when he grabs a tool and it's not what he needs, he immediately jumps to "OMG PHP's tool X doesn't do Y!" when only two more seconds of searching would reveal that if he pulled the Y tool out of the box, it would do exactly what he needs.

    It's obvious you didn't read the essay, but just skimmed the start. If you had, then you would have seen there are lots of very specific examples. It is much more than a 'poorly-supported opinion piece' and it's clear he has spent time with the language.

    I know next to nothing about PHP, having written a grand total of one script using it. But I do know several other languages and am always interested in learning more, so I read the entire (very long) article. What I saw is just plain scary. For specifics, check out the sections on operators, variables, and error handling. If even half of what he wrote is true then PHP is definitely not for me. I prefer my languages' rational features to outnumber their WTF's by a decent ratio. That said, I probably will still try it out for myself at some point when I have the time, but I'm not optimistic.

  • jmacpherson (unregistered)

    Funny to get this far and no reference to useless use of cat, so here it is, just so no one else will have to mention it. You're welcome!

  • (cs) in reply to M
    M:
    There's a difference between "likes" and "is prepared to use if paid well enough".
    +5. And I'm not even paid well enough :(
  • Caue (unregistered)

    TRWTF is PHP specially PHP scoping

  • Jim (unregistered) in reply to timok
    timok:
    when combined properly and sticking to standards (and well-tested frameworks), you can have as much confidence in your JavaScript as in your server-side code.
    Except that I can, and will, fuck with your JavaScript, because it runs on my computer, not yours. Keep that in mind as you code.
  • MadAdder (unregistered) in reply to Leo

    Say what you want about PHP and its developers, but at least PHP isn't a juxtaposition of tag-based markup and inline scripting interpreted by a program that is running on top of a Java Virtual Machine. Sure, there are a few more options for web-based development, but PHP provides the least barrier to entry. If that attracts more shoddy developers, so be it.

  • Mike (unregistered) in reply to Doug
    #ifdef MC

    #undef MC

    #endif

    #define MC(type, symbol, text) type##_OP

    typedef enum {

    #include "OpsMC.h"

    NUM_OPS

    } Op;

    (etc.)

    You won the unofficial dailywtf obfuscated C contest today. Congratulations!

  • pantsman (unregistered)

    It's just sad when people defend PHP. Ignorance must be bliss.

    I used to hold the job title "Senior PHP Developer". Learning a proper programming language and looking for a new job was the best thing I ever did for my career.

Leave a comment on “Loose Cat Handling”

Log In or post as a guest

Replying to comment #:

« Return to Article