Basic tech stuff

Programming and Linux administration

PHP is seriously sick

Posted by Daniel Brahneborg on 2007 March 21

It’s obvious that ‘true‘ should be considered true and ‘false‘ should be considered false in an if statement.

It’s sane that ‘1‘ should be true, but slightly annoying sometimes that ‘0‘ is false. Especially since '0==null‘ also evaluates to true. What idiot decided that?

Then it gets worse. The expression ‘array()‘ is also false, making it impossible to use “if ($variable)” to decide if the variable has been set to anything.

On top of that, ‘array()==null‘ also evaluates to true. WTF? Seriously?

After some searching, my little if statement now looks like this: “if (is_array($variable)) …”. Gee, thanks.

When coming from an object oriented world, an object is an object, and an explicitly created object is certainly different from null. That’s so fundamental it goes beyond saying.

Combined with the fact that a normal variable assignment always (unless you’re using “&=“) makes a deep copy of an array, my little loop to create a nested array of arrays, became a nightmare. It’s three times as long as the Ruby version, and with lots of duplicated code since I always have to populate each array before adding it into the containing object.

And people actually like this language? Why?

Andra bloggar om: , .

(Edit: Corrected the true/false thing found by Svava)

Advertisements

12 Responses to “PHP is seriously sick”

  1. 3mr (3 means A!) said

    man, why dont you use the triple equals not the double equals, since (===) compares the vale and the type! and the (==) only compares the value;
    for the arrays, the if statments should contain is_array() because there should be a problem in the foreach if you didnt check always.
    for the true and false; always use whats called: “safe coding”. and always double check everything.
    notice also, that the zero means false always and not a value unless you use the (===) or isset()
    Finally, we love PHP because it made our lives easier and for the wide and diverse use of it, and also the popularity and the easiness of learn.
    Bytheway, i see that you use wordpress and it is php 😉

    Amr Hourani.

  2. The triple equals sounds much more sane. Tons of thanks, my sanity is slowly returning. 🙂

    What made me angry this morning was that I thought it violated the principle of least surprise. Being a common and mature language, naturally the constructs that I needed are available. The difference between == and === was too difficult to find in the documentation, even if I understand the need for both. The “array() == null” goes against all other programming languages I know, which made things rather confusing.

    So. Tons of thanks for the tip. I still am a bit upset about the array-copy thing though. You don’t have a nice solution for that as well?

  3. 3mr (3 means A!) said

    Hi, thank you i am blushed 🙂
    fro coping array you just do:
    $new_array = $old_array;
    it is just simple like that..
    note: if you need to reference (point) the values to the old array, you just do that:
    $new_array = &$old_array;

    i hope my answer will help you, unless i didnt understand your question 🙂

    Amr Hourani
    Bytheways, i have a blog writing some coding basics and some security issues, if you are interested:
    http://3mro.wordpress.com

  4. svava said

    you lost me in the first sentence… why is it obvious that ‘false’ is true?

    or was it a freudian slip?

    i haven’t used neither php nor perl in a while obviously but i remember you can do some really neat things

    i especially liked the die instruction

  5. Svava: Yes, I meant true=true and false=false, of course. I agree, saying “do this or die” is charming, if nothing else.

  6. svava said

    it is also useful in bank robberies

    i’ve been told

    yet to be tried though

  7. You mean like this?

    – This is a bank robbery! Fill this bag with money, or I’ll show you my PHP code!
    – No, please no! Here, take it all!

  8. svava said

    wow you must really be a lousy php programmer

    glad i don’t own a bank

  9. ML said

    if ($bank==”robbed”)
    die();
    elseif ($php==”stupid”)
    print “true”;
    else
    print “php code”;

    Ah..the above would be true to be false, in a non-freudian way

  10. Liffon said

    Jag känner att en Anders och Måns-referens är befogad i sammanhanget:

    Om du inte fattade den får du söka om Anders och Måns och pingis på YouTube. 😛

  11. Liffon said

    Nej, php-koden försvann. Dumma wordpress. Här kommer den utan php-taggar:

    play($pingis)
    or die(“motherfucker”);

  12. Dynamo Dan said

    Bottom line, five years later PHP is still stupid this way. `$i = 0; if(empty($i)) { …` is another brilliant gem.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: