A new way of array checking in PHP



  • This code is lifted from a socket handling class that I've been asked to work on today. Prepare to cringe: 

    if (eregi('Invalid argument supplied for foreach() in /usr/cvs/projects/13xx/1371/Classes/XML/XML_inc.php on line 392',$v[0])) {
        return false;
    }

    It's worth noting that the foreach() statement will move from line 392 with so much as a return key (as it did today...). It's also worth noting that the path information is gloriously hard-coded. This was deemed dailywtf worthy in the office, and as such I present it to you now.

    Happy Wednesday :)



  • that is a big WTF, there are LOTS better ways to do this



  • Awesome!



  • You could wrap your foreach in an if(is_array($var))...

    Or... this.



     



  • @dawguk said:

    It's worth noting that the foreach() statement will move from line 392 with so much as a return key (as it did today...).

    What a silly oversight, when

    [code]if (eregi('Invalid argument supplied for foreach() in /usr/cvs/projects/13xx/1371/Classes/XML/XML_inc.php on line 392',$v[0]) || eregi('Invalid argument supplied for foreach() in /usr/cvs/projects/13xx/1371/Classes/XML/XML_inc.php on line 393',$v[0]) || eregi('Invalid argument supplied for foreach() in /usr/cvs/projects/13xx/1371/Classes/XML/XML_inc.php on line 394',$v[0]) || eregi('Invalid argument supplied for foreach() in /usr/cvs/projects/13xx/1371/Classes/XML/XML_inc.php on line 395',$v[0]))[/code]

    would have enhanced future maintainability by at least 4%.



  • Wow. It's no wonder PHP gets a bad name. That's just horrible. And I'm a PHP developer. I've written a few kludges in my time, but I'm quite certain I never wrote anything /that/ bad.

     



  • You should really be printing the array using printer_write() to a wooden table, scanning it....



  • @dawguk said:

    This code is lifted from a socket handling class that I've been asked to work on today. Prepare to cringe: 

    if (eregi('Invalid argument supplied for foreach() in /usr/cvs/projects/13xx/1371/Classes/XML/XML_inc.php on line 392',$v[0])) {
        return false;
    }

    It's worth noting that the foreach() statement will move from line 392 with so much as a return key (as it did today...). It's also worth noting that the path information is gloriously hard-coded. This was deemed dailywtf worthy in the office, and as such I present it to you now.

    Happy Wednesday :)

    Okay, this is the first time I've lol'd at this site in a very long time. You are an internet hero. 



  • wow.... simply wow... this one also caused me to not only lol but also made finally sign up for an account here (been reading for months) to slander the schmuck who put that in place.

     doesn't he know that a "strpos(Invalid argument supplied for foreach() in /usr/cvs/projects/13xx/1371/Classes/XML/XML_inc.php on line 392) === false" if statement is faster!!!

    *sigh* idiot

     ;)
     



  • @chadillac 

    Careful, this guy might have some sort of developmental diplomatic immunity. 

    Anyway, surely allowing elementary language errors to simply escape down the socket or whatever for the client to puzzle out rather than handling it locally is just is a masterpiece of load balancing? :-P

    You lot are just jealous of the greater design here.



  • @chadillac said:

    doesn't he know that a "strpos(Invalid argument supplied for foreach() in /usr/cvs/projects/13xx/1371/Classes/XML/XML_inc.php on line 392) === false" if statement is faster!!!

     But, but ... regular expressions are COOL!
     



  • What really makes me wonder, is that this error message is normally contained in an exception and is only visible in the output if it's not caught.  How did this exception end up in text form that the developer felt he needed to match?  He executed a separate page and let the next page check for an error message in the output? faint



  • (drops jaw)

    (after a while of consternation, picks it up from the floor)

    Wow.

    1. eregi used to match a static string
    2. eregi used instead of preg
    3. hardcoded path
    4. hardcoded line number of a part of code in _another script_
    5. relying on the particular pattern of error reporting

    If this isn't worth the main page, I don't know what is.

    BTW, the path should be /usr/cvs/projects/13xx/1337/... LOL



  • Of course, you are assuming that the path also never changes as well, especially as this looks like it has come out of CVS.....


Log in to reply