[ 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.

Filed in: Languages,Programming.


  1. Comment by acebone:


    if ( $var === ‘DONE’) {

    Do Black Magic Here


    Always use === when you want a strict comparison. Actually the ability to use == (loose comparison with type conversion) and === (strict comparison without typecomparison) is an advantage. But of course you have to know about it.

    Say you get a postvariable – it will always be a string, but you KNOW that the string will always contain a digit. Then it’s easier to just use == if you have to compare it to a numerical value.

    Conclusion: PHP is not braindamaged, it’s rather smart :)

    December 20, 2007 @ 15:28
  2. Comment by acebone:

    typecomparison was a typo – it should’ve read typeconversion

    December 20, 2007 @ 15:29
  3. Comment by Mark:

    Interesting. I hadn’t considered the usefulness in the case of a post variable, where this does make sense. I guess this is a case of knowing the language and knowing what it was designed for. Although it can still be maddening encountering this behavior unexpectedly for the first time. Thanks for that insight.

    December 20, 2007 @ 20:11
  4. Pingback from PHP Insanity II | Programmer’s Paradox:

    […] 0. In fact, anything other than an integer will return 0 (with the exception of a few strings – see part I of this series). But wait, I know what you’re thinking: 0 is in fact, an […]

    December 21, 2007 @ 20:42
  5. Comment by chuck:

    I, too, had not considered the usefulness of this weird-notion-of-equality that PHP has for GET/POST variables. It’s a good point, but since the majority of my PHP work doesn’t deal in GET/POST variables (AMFPHP remoting services for Flash/Flex applications), I haven’t needed this “kind” of equality. I guess that’s what happens when you try to make a “hypertext preprocessor” into a real programming language.

    It still remains that programmers on the whole are accustomed to == being an equality operator, that it means, “equal,” not “kinda equal” or “equal in a POST-variable sense”. Hence this is still a bad design — “equal in a POST variable sense” ought to have used some other operator. “===” feels like it means “more equal” rather than “sorta equal” though, so I’d suggest ?= 😀

    December 24, 2007 @ 16:16
  6. Pingback from The Maybe Operator | Programmer’s Paradox:

    […] discussion stems from several events coming together in my brain at once.  First was my previous PHP Insanity post, part I.  In it I talked about PHP’s comparison operators and how I find them lacking.  This lead […]

    December 26, 2007 @ 22:37