The legacy-PHP-project-to-pay-my-future-trip strikes again.

WHY NOT !empty($var) OMGGGGGGG

  • 5
    I see you found a shitfest if a project. Welcome to the wonderful world of legacy php.
  • 3
    @C0D4 oh I did HAHAHA
    Every day I complain about this shitfest I remember it's almost over and it's going to pay for a wonderful trip.
  • 0
    Probably written when empty wasnt a thing
    I wouldve guessed this is used to explicitly check for an empty but theres no ===and also no string typehint so hello legacy my old friend
  • 3

    Empty doesn't mean the same as the string comparison.

    Empty does a whole lot more...


    Especially null can bite you.
  • 0
    @ThatPerlDeb yeah, I tried to guess all sorts of things, even your suggestion - but nope, empty() exists since PHP4 (at least).

    I decided to run a usage search. Turns out all uses are with array_filter(). But turns out array_filter() ALREADY filters our falsey values! Any surprise on this getting worse as I dig deeper? hahaha
  • 0
    @IntrusionCM sorry, what?

    null == "" is the same as empty("") or empty(null).

    Of course empty() is not the same for ALL cases, but PHP type-juggling allows us to do that kind of replacement without much judgment in 99% of the cases...
  • 1
    @igorsantos07 i said "CAN bite u". Not will bite u in this case. ;)

    In ur case "array()" would lead to different results:

    $vars = [null, "", array()];
    foreach($vars as $var)
    var_dump($var != "");

    NULL bool(false) bool(false)
    string(0) "" bool(false) bool(false) array(0) { } bool(true) bool(false)
  • 0
  • 0
    @IntrusionCM sidetracking... wouldn't it be easier if DevRant actually had markdown formatting?
    I mean... Every dev knows markdown, right? lol
Add Comment