26

According to PHP documentation, the following expressions return true when calling empty($var)

  • "" (an empty string)
  • 0 (0 as an integer)
  • 0.0 (0 as a float)
  • "0" (0 as a string)
  • NULL
  • FALSE
  • array() (an empty array)
  • $var; (a variable declared, but without a value)

I've found how to "solve" the problem by using empty($var) && $var != 0 but why php developers did it?

I think it is ridiculous, suppose you have this code:

if (empty($_POST["X"])) {
    doSomething();
}

I think "0" is not empty, empty is when there is nothing!

Maybe it's better to use

if (isset($x) && x != "") {//for strings
    doSomething();
}
6
  • 2
    Because 0 also means false. Commented Aug 3, 2014 at 1:51
  • 16
    I don't understand the downvotes, it's a legitimate question since "empty" implies a lack of data, whereas "0" is a legitimate string. I'm interested to know the reasoning behind this as well. Commented Aug 3, 2014 at 1:55
  • 2
    Because this is how it is defined. It could have been differently, but it is/was not. One might blame perl's false-y value conversions as the basis of the inspiration .. Commented Aug 3, 2014 at 1:58
  • @user2864740 Both the original author and myself understand that, the question is why empty is defined like this, since "0" is quite clearly not empty. Commented Aug 3, 2014 at 1:59
  • 2
    because "" == 0 == 0.0 == "0" == null == false == array() == $var. php is loosely typed. Commented Aug 3, 2014 at 2:08

1 Answer 1

21

empty roughly mirrors PHP's selection of FALSE-y values:

When converting to boolean, the following values are considered FALSE:

  • the boolean FALSE itself
  • the integer 0 (zero)
  • the float 0.0 (zero)
  • the empty string, and the string "0"
  • an array with zero elements
  • ...

As far as why PHP works this way, or why the empty function followed suit - well, that's Just The Way It Is.

Consider using strlen($x) (this is especially well-suited to sources like $_POST which are all string values) to determine if there is a non-empty string, including "0".

The final form I use would then be: isset($x) && strlen($x), with any additional processing applied knowing there was some post data.

Sign up to request clarification or add additional context in comments.

3 Comments

The use of quotes do majorily affect the data type of the actual value. It's worth a note also
thanks, but about booleans i have the same question, why "0" is evaluated as false? but it doesn't matter, i'm goint to solve it by using strlen or == "" or whatever... i was wondering why php developers designed php in that way
@user2961204 Because it is how PHP is - there need not be rational rationale, and the choice is history. Barring finding an original manifesto or design/notes, such decisions are generally lost and do not make good SO questions. Every language establishes values of truth/false-y, some stricter than others: e.g. Python/Ruby adopt much narrower definitions of false-y values. I suspect that PHP was heavily influenced by Perl (a dominate competitor at the time), which has similarly "quirky" values.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.