118

Could you help me to improve my coding style?:) In some tasks I need to check - is variable empty or contains something. To solve this task, I usually do the following.

Check - is this variable set or not? If it's set - I check - it's empty or not?

<?php $var = '23'; if (isset($var)&&!empty($var)){ echo 'not empty'; }else{ echo 'is not set or empty'; } ?> 

And I have a question - should I use isset() before empty() - is it necessary? TIA!

2
  • empty is !isset($var) || $var == false Commented Oct 19, 2015 at 6:37
  • 3
    in my oppinion empty() is a horrific development of PHP. As it consideres "0" in any forms as "empty" you may fall into bad traps costing lots of debugging time. I'd say: avoid at all cost and just write the logic yourself Commented Nov 4, 2016 at 3:45

6 Answers 6

161

It depends what you are looking for, if you are just looking to see if it is empty just use empty as it checks whether it is set as well, if you want to know whether something is set or not use isset.

Empty checks if the variable is set and if it is it checks it for null, "", 0, etc

Isset just checks if is it set, it could be anything not null

With empty, the following things are considered empty:

  • "" (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 $var; (a variable declared, but without a value in a class)

From http://php.net/manual/en/function.empty.php


As mentioned in the comments the lack of warning is also important with empty()

PHP Manual says

empty() is the opposite of (boolean) var, except that no warning is generated when the variable is not set.

Regarding isset

PHP Manual says

isset() will return FALSE if testing a variable that has been set to NULL


Your code would be fine as:

<?php $var = '23'; if (!empty($var)){ echo 'not empty'; }else{ echo 'is not set or empty'; } ?> 

For example:

$var = ""; if(empty($var)) // true because "" is considered empty {...} if(isset($var)) //true because var is set {...} if(empty($otherVar)) //true because $otherVar is null {...} if(isset($otherVar)) //false because $otherVar is not set {...} 
Sign up to request clarification or add additional context in comments.

5 Comments

It's mean - in the code above I check is variable set or not twice?:)
Just check whether it is empty, php won't throw an error if this is not the case.
You are omitting the biggest point of empty: It doesn't throw a warning when the tested variable does not exist. That's the whole point of this function, otherwise it's identical to == false.
So, the fact in manual that "no warning is generated when the variable is not set" confused me. No warning doesn't mean that I will not have troubles in the code below. Now it's clear for me. Thx alot!
"isset just checks if is it set, it could still be null" That's not true: isset on a null (and existing) variable will evaluate to false.
14

In your particular case: if ($var).

You need to use isset if you don't know whether the variable exists or not. Since you declared it on the very first line though, you know it exists, hence you don't need to, nay, should not use isset.

The same goes for empty, only that empty also combines a check for the truthiness of the value. empty is equivalent to !isset($var) || !$var and !empty is equivalent to isset($var) && $var, or isset($var) && $var == true.

If you only want to test a variable that should exist for truthiness, if ($var) is perfectly adequate and to the point.

1 Comment

This one explains much much better. If we get comparison for all other cases like this !empty is equivalent to isset($var) && $var, it would be awesome. Thanks @deceze
7

You can just use empty() - as seen in the documentation, it will return false if the variable has no value.

An example on that same page:

<?php $var = 0; // Evaluates to true because $var is empty if (empty($var)) { echo '$var is either 0, empty, or not set at all'; } // Evaluates as true because $var is set if (isset($var)) { echo '$var is set even though it is empty'; } ?> 

You can use isset if you just want to know if it is not NULL. Otherwise it seems empty() is just fine to use alone.

3 Comments

Explanation for the down vote? He's not asking if they are the same, he's asking if he needs to check isset before checking empty.
I didn't downvote, but "use isset if you want to know it is not null" is not correct: $var = null; isset( $var ) == true.
From the PHP5/4 Manual: isset() - "Determine if a variable is set and is not NULL." us.php.net/manual/en/function.isset.php
2

Here are the outputs of isset() and empty() for the 4 possibilities: undeclared, null, false and true.

$a=null; $b=false; $c=true; var_dump(array(isset($z1),isset($a),isset($b),isset($c)),true); //$z1 previously undeclared var_dump(array(empty($z2),empty($a),empty($b),empty($c)),true); //$z2 previously undeclared //array(4) { [0]=> bool(false) [1]=> bool(false) [2]=> bool(true) [3]=> bool(true) } //array(4) { [0]=> bool(true) [1]=> bool(true) [2]=> bool(true) [3]=> bool(false) } 

You'll notice that all the 'isset' results are opposite of the 'empty' results except for case $b=false. All the values (except null which isn't a value but a non-value) that evaluate to false will return true when tested for by isset and false when tested by 'empty'.

So use isset() when you're concerned about the existence of a variable. And use empty when you're testing for true or false. If the actual type of emptiness matters, use is_null and ===0, ===false, ===''.

Comments

1

Empty returns true if the var is not set. But isset returns true even if the var is not empty.

1 Comment

This is a somewhat misleading and incomplete answer.
-4
$var = 'abcdef'; if(isset($var)) { if (strlen($var) > 0); { //do something, string length greater than zero } else { //do something else, string length 0 or less } } 

This is a simple example. Hope it helps.

edit: added isset in the event a variable isn't defined like above, it would cause an error, checking to see if its first set at the least will help remove some headache down the road.

2 Comments

strlen under zero? I wanna see that string.
@deceze Just a rough example :) OK use =0 not <0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.