The PHP empty function is wrong!!

After 20  minutes debugging code (BTW, what a great tool are debuggers), and you realize that the bug that manifests on A, does not come from A, not B, C or D, but it comes from far far away, something like Z you realize, as may other times, that a “bug” in a remote place could have lots of side-efects.

Anyway, after 20 minutes debugging I have discovered a but on a website in PHP I’m developing, and when I realiced it was because a conceptual problem in a PHP function I’ve was using, then I got angry (but it was my fault).

The PHP function I’m talking about is empty, which as the manual states, determines whether a variable is empty (the concept is quite easy, I don’t understand why is not well implemented).

The problem was that I have a conditional saying if empty ($str) then do X, else do Y.

The conceptual problems comes here:  empty(“0”) is true!!

<dramatism>For God’s sake! What the hell was thinking the person who programed this?</dramatism>

Anyway, yesterday I read this blog post about suggesting solutions and not only pointing out problems, so let’s describe the problem, and let’s give a solution for all of this.

Let’s see the definition as it appears on the PHP manual:

emptyDetermine whether a variable is empty

The following things are considered to be empty:

  • “” (an empty string)
  • 0 (0 as an integer)
  • “0” (0 as a string)
  • NULL
  • FALSE
  • array() (an empty array)
  • var $var; (a variable declared, but without a value in a class)

I undestand, according to the definition, that a NULL variable is considered as empty, the same with the FALSE value, the same as an empty array array() , and the same  with the string (which I think everyone understands why is empty). Even, I understand the number 0 is considered empty. Up to this point everything is consistend with the definition, it makes sense that each data type (boolean, string, array, reference) has a unique value considered as empty.

What it does not make sense is the strings having two values considered as empty. The empty string is ”, and that’s it.

I can imagine the reason why “0” is considered as empty. In PHP you could do 322 + “17” and internally it will transform the string “17” in the number 17, so it will compute the numeric addition. With this in mind, when “0” is passed to that function, there is no feasible way to know if the programmer is passing the string “0” or the number 0 which forgot to cast to an integer.

Conclussion: the function empty should not contemplate the case of ‘0’ as true. It is wrong to assume that the programmer  is stupid enough to  forget to do a cast, and forces good people (like  me :D), that understands what empty means and knows how to cast a variable in PHP, to avoid that implementation.

The original bug was my fault for using a function without knowing its behaviour in all cases, but IMHO that function should behave in another way.

Trackback URL

  1. Croc
    29/03/2012 at 2:02 pm Permalink

    I totally agree. The worst thing is that the father of PHP – Rasmus Lerdorf – uses empty() for checking whether data has been sent in his book “PHP5”. This is so ridiculous and sing of being not-too-good programmer…