[ Content | View menu ]

PHP Insanity

Mark Mzyk | December 19, 2007

I’m sure what I’m about to say will be familiar to all you PHP programmers out there.  PHP is a crap language, as far as languages go.  Of course, I feel I can say this since I program in PHP everyday for my job.

Why is PHP a crap language?  It’s inconsistent.  It routinely does things one wouldn’t expect.  Let’s back this assertion up with an example.

We have some code, an if statement, and it goes like this:

if ( $var == 'DONE') {
    Do Black Magic Here
}

Now, $var has a number of values it can take.  Due to some poor coding by our predecessor and just general bad luck on our part coming after him , $var can take the values 0,1,2, and DONE.  Yes, I know that choice was very poor, but we have to work within the context of the system as it is presented to us.So what happens here? This statement never executes unless $var is set to DONE, correct?

Of course that isn’t correct.  If it was, this post wouldn’t be titled PHP Insanity.  It fact, if $var is set to 0, black magic will happen.

But wait, 0 doesn’t equal DONE!  Well, since PHP at times has the mental capacity of a two year old, it does.  Almost any sting applied to an evaluation like the one presented will be evaluated to 0 (assuming it is being compared to a number) , so if the variable the string is being compared to is equal to 0, black magic happens.

Note that I said almost any string.  There is, of course, an exception.  If a string begins with a digit or series of digits, PHP will use those digits in the evaluation.  So if instead of DONE, say we had 12DONE.  If $var is set to 0, the comparison is 0 == 12, which is false.  Now, if the digits are not at the beginning of the string, then the string still evaluates to 0.  So DO12NE == 0 in PHP’s eyes.

Insanity?  I think so.  It’s enough to push me to the edge.  But if you think I’m completely off base here, or that this is somehow sane, let me know.