'0' as a string with empty() in PHP '0' as a string with empty() in PHP php php

'0' as a string with empty() in PHP


You cannot make empty() take it. That is how it was designed. Instead you can write an and statement to test:

if (empty($var) && $var !== '0') {    echo $var . ' is empty';}

You could use isset, unless of course, you want it to turn away the other empties that empty checks for.


You cannot with empty. From the PHP Manual:

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)

You have to add an additional other check.


You can't with only empty(). See the manual. You can do this though:

if ($var !== '0' && empty($var)) {   echo "$var is empty and is not string '0'";}

Basically, empty() does the same as:

if (!$var) ...

But it doesn't trigger a PHP notice when the variable is not set.