• RHuckster (cs)

    ...$denominator = TRUE;

    if ($denominator == 1 || $denominator == -1) ...

    WTF?!!

  • Divine Math (unregistered)

    Such a blasphemy. Everyone knows that dividing by zero results in a collapse of all known realities. Average to zero? That's the summary of our existance.

    Captch: nobis - that PHP god is such a nobis.

  • XXXXX (unregistered)

    At one time was the PHP God an accountant for the government? Or did he get his start massaging research results to justify grant money?

  • Satan (unregistered)

    trwtf is religious belief

  • BlackBart (unregistered)

    Finally, a way to safely_divideByZero(4.5, 0.0)

    "PHP ERR -00425 division by zero"

  • Chris (unregistered)

    The flowchart does not match the code.

  • Germstore (unregistered)

    Yospos, bitch. TBC forever.

  • Jacques Beauregard (unregistered) in reply to BlackBart
    BlackBart:
    Finally, a way to safely_divideByZero(4.5, 0.0)

    According to the documentation this should been written as safely_divideByZero(4.5, "php")

  • The_Abominator (unregistered)

    PHP god? This guy needs to be sent to Hades.

    Anyone who adds comments indicating how clever their code is, should usually be avoided, shot or buried naked and waist down in a red ant pit.

    At the end of that cluster of dog piss that he calls a function, the only possible comment is "Whats the F'ing Point"?

    If I saw this code, I'd crucify the coder, without burial so he won't rise again.

    But then again, it is PHP, don't they all think they are gods...'wink'

  • Wiz (unregistered)

    Amusingly enough I once had a colleague who had to compute some ratio to be displayed as percentage. I asked him to throw IllegalArgumentException if the denominator was 0 but he decided to go another way, I think he didn't listen to what I said and tried to solve himself the case where the denominator was 0. He just replaced the denominator with 1 if it was 0 and wondered why the result sometimes was not between 0 and 100% but around one million %.

  • Sunday Ironfoot (cs)

    Unless I'm fundamentally misunderstanding PHP, surely the only code that will EVER run is the line:

    $result = $numerator/$denominator;

    ...since if $denominator == "php" then it'll fail the second check and the line above just runs (in PHP $denominator can't both be equal to TRUE AND "php" can it?). Whereas if $denominator != "php" then it'll just run the above line of code again.

    Surely this PHP God has an EPIC misunderstanding of how variables work.

  • AA (unregistered)

    Though I guess he got one thing right.

    Any bad things are == "php".

  • Anonymous (unregistered) in reply to Chris

    And is also inconsistent by itself.

  • eVil (unregistered)

    TRWTF:

    Simply imagining the concept of dividing by zero didn't immediately make the guy need a quiet lie down.

  • AA (unregistered) in reply to Sunday Ironfoot
    Sunday Ironfoot:
    (in PHP $denominator can't both be equal to TRUE AND "php" can it?)

    Clearly you don't know PHP.

    0 == "php" "php" == TRUE TRUE == 1

  • too_many_usernames (cs) in reply to RHuckster

    The RWTF regarding divide by zero is that people forget (or, more likely, were never taught) that division by zero does not indicate an arithmetic error. If you get into a situation where you are dividing by zero it means you have a model error, because division by zero indicates "your equation no longer provides useful information."

    Checking the division operation is too late; if you have a divisor of zero that means you need to pick a different equation, not throw an exception.

  • eVil (unregistered) in reply to Sunday Ironfoot
    Sunday Ironfoot:
    Unless I'm fundamentally misunderstanding PHP, surely the only code that will EVER run is the line:

    $result = $numerator/$denominator;

    ...since if $denominator == "php" then it'll fail the second check and the line above just runs (in PHP $denominator can't both be equal to TRUE AND "php" can it?). Whereas if $denominator != "php" then it'll just run the above line of code again.

    Surely this PHP God has an EPIC misunderstanding of how variables work.

    I think (i'm no expert) that the $denominator variable, containing the string "php", will be implicitly cast to a boolean, and a string cast to a boolean results in 1.

    Basically, the guy is relying on some rather shifty non-typesafe casting behaviour in php, that should probably be avoided at all costs.

  • AA (unregistered) in reply to too_many_usernames
    too_many_usernames:
    The RWTF regarding divide by zero is that people forget (or, more likely, were never taught) that division by zero does not indicate an arithmetic error. If you get into a situation where you are dividing by zero it means you have a model error, because division by zero indicates "your equation no longer provides useful information."

    Checking the division operation is too late; if you have a divisor of zero that means you need to pick a different equation, not throw an exception.

    The way it's supposed to work is that you throw an exception, and then something at a higher level of logic that knows how to handle it appropriately (for instance, by picking a different equation to try in this case) can do so.

  • AA (unregistered) in reply to AA
    eVil:
    Basically, the guy is relying on some rather shifty non-typesafe casting behaviour in php, that should probably be avoided at all costs.

    PHP should basically be avoided at all costs.

  • David Wright (unregistered)

    Are you SURE the PHP God is a complete idiot? Isn't it possible that this code was intended as a joke?

    Maybe he wrote it to confuse his replacement - or maybe because some manager, unhappy about complaints that customers got errors when they entered 0.0, ordered him to do a divide-by-zero function, and he did so in the most dumbly awkward way he knew?

    CAPTCHA genitus - an attempt at genius which went wrong. Just like this code, perhaps.

  • frits (cs)
    PHP God:
    /* if the denominator == "php" we  can safely set the value of the denominator to "php"  */
    

    /* if the denominator == TRUE we can set the value of the denominator to TRUE */

    /* this part is clever, we don't know which is the right answer, so we can use a for loop to randomize */

    /* we don't want zero as the answer (back to square one if it is!) so we randomize from 0 to 2, but the for loop throws out results that == 0 and makes it try again */ $n = rand(0,2);

    /* if the answer == 1, then we set the denominator to 1, somewhat obvious here */

    /* we return as the result a simple division, except now we can divide by zero without error */

    .

    What's all this "we" shit? I can guarantee that no one else was involved in this work of art.

  • evilspoons (cs)

    This is quite possibly the most retarded piece of code I've seen on here in ages. I am at a loss for words.

  • tragomaskhalos (unregistered) in reply to David Wright
    David Wright:
    Are you SURE the PHP God is a complete idiot? Isn't it possible that this code was intended as a joke?

    Maybe he wrote it to confuse his replacement - or maybe because some manager, unhappy about complaints that customers got errors when they entered 0.0, ordered him to do a divide-by-zero function, and he did so in the most dumbly awkward way he knew?

    CAPTCHA genitus - an attempt at genius which went wrong. Just like this code, perhaps.

    Yeah that's what I was thinking - it seems a little too knowing/tongue in cheek to have been written by the complete idiot that the logic would suggest authored it.

  • Hasteur (cs)

    I program in PHP professionally, and I suspect that a significant portion of the code was redacted to protect the incompetent.

    The forced type coersion puts this in hell such that if there is a string that is equivilant to php, you climb all the way down the rabbit hole that will make any invocation of this routine to go down the -1 path.

  • tom103 (unregistered)

    I just realized that "php" is REALLY equal to 0 in PHP... and it is also equal to TRUE ! TRWTF is PHP...

  • MC (unregistered) in reply to David Wright

    Definitely a joke. This guy was just messing with anybody that actually looked at his code.

  • Ru (unregistered)

    Historically, gods were seen as callous, uncaring, often unjust and their actions often appeared arbitrary or even downright malicious to mere mortals.

    The traditional solution to such beings was to put their followers to fire and sword, tear down their temples and make enthusastic attempts to expunge their name from history. I feel this process has as much value today as it has done for thousands of years, and is a sadly underused business practise.

  • QJo (unregistered)

    Aha, I think see what TRWTF is, it's right in the middle, where it suggests that the program flow goes back to "square one". There is no square one. But there's lots of round zeroes, which is what we are trying to avoid. So all we need to do is get the code to do to a square one and we're done. Am I right?

  • beentheredonethat (unregistered)

    Took me a while to realize that the variable type of $denominator changed with each scope, hence why the assignment and the reason that we actually get a result. This would be redundancy to an ordinary programmer, but this "god" knew a few tricks about dynamic variables. Sad that he never learned programming ...

  • Migala (unregistered) in reply to AA
    AA:
    Sunday Ironfoot:
    (in PHP $denominator can't both be equal to TRUE AND "php" can it?)

    Clearly you don't know PHP.

    0 == "php" "php" == TRUE TRUE == 1

    So basically this guy has just proven that 0 == 1. That, to me, is one brillant miracle.

  • Sam (unregistered)
    Comment held for moderation.
  • Ryan (unregistered) in reply to AA
    AA:
    Sunday Ironfoot:
    (in PHP $denominator can't both be equal to TRUE AND "php" can it?)

    Clearly you don't know PHP.

    0 == "php" "php" == TRUE TRUE == 1

    Incorrect on the first one... I think the writer of the code was expecting someone to pass in the string "php" as the denominator value.

    There are several languages where any non-empty string, or integer greater than 0, will evaluate to true. Strict type checking is required to differentiate. === vs ==.

    These all evaluate to true:

    0 != "php" "php" == true "php" !== true true == 1

    All that said, the writers idiocy is not up for debate. This is some of the most appalling code I've ever seen in ANY language.

  • woah (unregistered) in reply to Migala
    Migala:
    AA:
    Sunday Ironfoot:
    (in PHP $denominator can't both be equal to TRUE AND "php" can it?)

    Clearly you don't know PHP.

    0 == "php" "php" == TRUE TRUE == 1

    So basically this guy has just proven that 0 == 1. That, to me, is one brillant miracle.

    I think I'm going to have an aneurysm, just looking at that. PHP needs to die in a fire.

  • synp (unregistered) in reply to frits

    It's the royal plural.

    Besides, it's more enterprisey. You're not just writing this for yourself. There's this huge corporation with its thousands of employees standing behind you on this.

    If you're working for the government, 'we' is the entire $nationality people. How's that for awesome.

    captcha: uxor - unsigned XOR. That's some advanced bit logic

  • Code Slave (cs)

    All I can say to that is "OW!!!!". I think I'm having a stroke.

  • QJo (unregistered) in reply to Code Slave
    Code Slave:
    All I can say to that is "OW!!!!". I think I'm having a stroke.

    Hmmm ... you have a stroke if you like, I'm going to have a jolly good feel.

  • Ken B. (unregistered) in reply to David Wright
    David Wright:
    Are you SURE the PHP God is a complete idiot? Isn't it possible that this code was intended as a joke?

    Maybe he wrote it to confuse his replacement - or maybe because some manager, unhappy about complaints that customers got errors when they entered 0.0, ordered him to do a divide-by-zero function, and he did so in the most dumbly awkward way he knew?

    function safely_divideByZero($numerator,$denominator)
    {
    if ( ($denominator + 0.0) == 0.0 )
    return 42;
    else
    return $numerator / $denominator;
    }

  • Ben (unregistered) in reply to Sam

    Anytime you have to explain a joke it's instantly a failure.

  • Pr0gramm3r (unregistered)

    TRWTF is nobody here knows the correct solution to the problem,

     $x/0 = INF 
  • RichP (cs)

    This comment is clever.

  • mernen (cs)

    The flowchart has the loop arrows for True/False inverted, and most if decisions are missing a "False" arrow.

    Furthermore, I agree with David Wright and MC. This code is just a disingenuous demonstration of PHP type coercion rules.

  • Jeff (unregistered)

    Wow! This is the first one in quite some time that had me muttering "this guy is insane!" With a mind like that, how could he produce any code that worked? Ever?

    The suggestion that this might have been a practical joke to confound mere mortals had me grasping at a glimmer of hope for a moment there. I thought maybe there is a future for the species after all. Then I realized, no, there isn't.

    Props to one of the finer WTFs in a while. Like a fine wine. Which, of course, is just rotten grape juice that has been sitting around for a decade.

  • Anon (unregistered) in reply to frits

    What's all this "we" shit? I can guarantee that no one else was involved in this work of art.

    It's not unusual in technical writing, where "we" refers to "you and I, dear reader".

  • boog (cs)
    ...sometimes this will return 25, sometimes -25. You'll notice that these values average out to 0, which is exactly what we were trying to divide by...
    I'm so going to try this excuse some time.

    User: The application's not working.

    boog: Yes, but sometimes it's working really well. You'll notice that these occurrences average out to mostly working, which is exactly what you were expecting. No need to thank me, just doing my job.

  • C-Octothorpe (cs) in reply to frits
    frits:
    What's all this "we" shit? I can guarantee that no one else was involved in this work of art.

    Just imagine him holding your hand walking you through a house of horrors, describing in excrutiating detail the thoughts going through his tortured and demented mind. Imagine a place so twisted that you can "cleverly" divide an apple amongst zero people...

    Either that or he was just a moron with multiple personality disorder and was describing this for his "followers".

  • P (unregistered) in reply to Pr0gramm3r
    Pr0gramm3r:
    TRWTF is nobody here knows the correct solution to the problem,
     $x/0 = INF 

    Actually

     $x > 0 ? INF : $x < 0 ? -INF : NaN 
  • port22 (unregistered)

    I divided by zero and all I got was this headache.

  • mernen (cs) in reply to Ryan

    For someone who doesn't even know the language you're talking about, you do speak with a lot of authority.

  • Ryan (unregistered) in reply to Ryan
    Ryan:
    AA:
    Sunday Ironfoot:
    (in PHP $denominator can't both be equal to TRUE AND "php" can it?)

    Clearly you don't know PHP.

    0 == "php" "php" == TRUE TRUE == 1

    Incorrect on the first one... I think the writer of the code was expecting someone to pass in the string "php" as the denominator value.

    There are several languages where any non-empty string, or integer greater than 0, will evaluate to true. Strict type checking is required to differentiate. === vs ==.

    These all evaluate to true:

    0 != "php" "php" == true "php" !== true true == 1

    All that said, the writers idiocy is not up for debate. This is some of the most appalling code I've ever seen in ANY language.

    I just correct my previous statement: We, in the office here, just tested that in PHP the integer 0, does indeed equal any string, but any (non empty) string also equals TRUE. An empty string also equals FALSE. It appears that PHP contains a rather large logical fallacy when it comes to these sorts of logical tests. In short, he could have used ANY string instead of "php" in the WTF function. I feel like a noob.

  • Larry (unregistered) in reply to Anon
    Anon:
    What's all this "we" shit?
    It refers to the cooperation between two minds: mine and the computer's. I'm the coach, it's following my instructions:
    First we contemplate the arguments, not that we're going to do anything with them.
    Then we should prepare for output.
    We should say "Hello, World!"
    We want to move the cursor to a New Line.
    Now we're done.
    See?

Leave a comment on “Divine by Zero”

Log In or post as a guest

Replying to comment #:

« Return to Article