php: '0' as a string with empty()-ThrowExceptions

Exception or error:

I want a 0 to be considered as an integer and a ‘0’ to be considered as a string but empty() considers the ‘0’ as a string in the example below,

$var = '0';

// Evaluates to true because $var is empty
if (empty($var)) {
    echo '$var is empty';
}

how can I ‘make’ empty() to take ‘0’s as strings?

thanks.

How to solve:

You cannot make 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.

Edit

Fixed the check to do a type check as well, thanks to the 2371 for pointing that out 🙂

Answer:

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.

Answer:

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 doesn’t trigger a PHP Notice when the variable is not set.

Answer:

You can’t. From the manual

Returns FALSE if var has a non-empty and non-zero value.

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)

Answer:

PHP has different functions which can be used to test the value of a variable. Three useful functions for this are isset(), empty() and is_null(). All these function return a boolean value. If these functions are not used in correct way they can cause unexpected results.

isset() and empty() are often viewed as functions that are opposite, however this is not always true. In this post I will explain the differences between these functions.

isset()

From PHP manual – isset():

isset — Determine if a variable is set and is not NULL

In other words, it returns true only when the variable is not null.
empty()

From PHP Manual – empty():

empty — Determine whether a variable is empty

In other words, it will return true if the variable is an empty string, false, array(), NULL, “0?, 0, and an unset variable.
is_null()

From PHP Manual – is_null():

is_null — Finds whether a variable is NULL

In other words, it returns true only when the variable is null. is_null() is opposite of isset(), except for one difference that isset() can be applied to unknown variables, but is_null() only to declared variables.

The table in picture is an easy reference for what these functions will return for different values. The blank spaces means the function returns bool(false).
enter image description here

Also i have made custom function for checking all stuff.

function checkEmpty($var, $term = ""){
    if(isset($var) && trim($var) != "" && (!empty($var) || trim($var) == 0)){
        return true;
    }
    else{
        if($term != ""){
            return array("status" => "error", "desc" => "$term can not be empty");
        }
        else{
            return array("status" => "error", "desc" => "value can not be empty");
        }
    }
}

Answer:

In both of your cases empty() will return true. Check the doc – http://php.net/empty.

I suggest using a different function to match your spec.

Answer:

I always add to my codebase

function is_blank($value) {
    return empty($value) && !is_numeric($value);
}

and use it instead of empty(). It solves the issue of keeping zeros (int, float or string) as non-empty.

See http://www.php.net/manual/en/function.empty.php#103756 which was added May 2011.

Answer:

$var = '0';

// Evaluates to true because $var is empty
if (empty($var) && $var !== '0') {
    echo '$var is empty or the string "0"';
}

Answer:

empty is by far the most confusing and useless function in the php repertoire. Don’t use it.

There are three separate things you want to know when checking a value.

  • the value exists (use isset)
  • the value has a specific type (use is_xxx)
  • the value has specific properties (use comparison operators, strpos or regular expressions).

(the last two can be combined into one with typecasts or ‘===’).

Examples:

if(isset($var) && is_string($var) && strlen($var) > 0)...
if(isset($var) && intval($var) > 0)...
if(isset($var) && $var === '0')...

This seems more verbose, but shows clearly what you’re doing. For structural objects it often makes sense to have a shortcut getter, e.g.

 /// get a string
 function s($ary, $key, $default = '') {
     if(!isset($ary[$key])) return $default;
     $s = trim($ary[$key]);
     return strlen($s) ? $s : $default;
 }
 /// get a natural number
 function n($ary, $key, $default = 0) {
     $n = intval(s($ary, $key));
     return $n > 0 ? $n : $default;
 }

 $name = s($_POST, 'name');
 $age  = n($_POST, 'age');

Answer:

In this case don’t use empty, use isset() in place of it. This will also allow 0 as an integer.

$var = '0';
if (!isset($var)) {
    print '$var is not set';
}

$var = 0;
if (!isset($var)) {
    print '$var is not set';
}

Neither should print anything.

Answer:

You can not, because integer,string,float,null does not matter for PHP.

Because it is cool 🙂

You must check characteristic features your variable is_numeric,isset,===,strlen, etc.

For example:

if (strlen(@$var)==0) {
    echo @$var . ' is empty';
}

or

if (@$var==="" || !isset($var)) {
    echo @$var . ' is empty';
}

or other examples 🙂

thanks for reading.

Answer:

If you want skip empty $filter and dont skip $filter = ‘0’ and others
values

$filter = ''; //or $filter = '0'; or $filter = '1';

//trim the $filter

if( isset($filter) and ( $filter != '' or $filter == '0') ) {

//$filter data 

};

Answer:

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

Leave a Reply

Your email address will not be published. Required fields are marked *