I see a lot of people using a variety of different methods to check whether of a variable is empty, there really seems to be no consensus. I've heard that if($foo)
is exactly the same as if(!empty($foo))
or if($foo != "")
. Is this true?
I realize it's a really simple question, but I'd really like to know. Are there any differences? Which method should I use?
if($foo != "")
is equivalent to if($foo)
most of the time, but not always .
To see where the differences are, consider the comparison operator behavior along with the conversion to string rules for the first case, and the conversion to boolean rules for the second case.
What I found out is that:
$foo === array()
, the if($foo != "")
test will succeed (arrays are "greater than" strings), but the if($foo)
test will fail (empty arrays convert to boolean false
) $foo === "0"
(a string), the if($foo != "")
test will again succeed (obviously), but the if($foo)
test will fail (the string "0"
converts to boolean false
) $foo
is a SimpleXML object created from an empty tag, the if($foo != "")
test will again succeed (objects are "greater than" strings), but the if($foo)
test will fail (such objects convert to boolean false
) See the differences in action .
The preferred method to test is if(!empty($foo))
, which is not exactly equal to the above in that:
if($foo != "")
(which IMHO is simply horrible). E_NOTICE
if $foo
is not present in the current scope, which is its main advantage over if($foo)
. There's a caveat here though: if $foo === '0'
(a string of length 1) then empty($foo)
will return true
, which usually is (but may not always be) what you want. This is also the case with if($foo)
though.
Finally, an exception to the above must be made when there is a specific type of value you want to test for. As an example, strpos
might return 0
and also might return false
. Both of these values will fail the if(strpos(...))
test, but they have totally different meanings. In these cases, a test with the identical operator is in order: if(strpos() === false)
.
No it's not always true. When you do if($foo)
PHP casts the variable to Boolean. An empty string, a Zero integer or an empty array will then be false
. Sometimes this can be an issue.
You should always try to use the most specific comparison as possible, if you're expecting a string which could be empty use if($foo==='')
(note the three equal signs). If you're expecting either (boolean) false or a resource (from a DB query for instance) use if($foo===false){...} else {...}
You may read the documentation about casting to boolean to find the answer to this question. There's a list in there with which values are converted to true
and false
, respectively.
Note that empty also checks if the variable is set, which regular comparison does not. An unset variable will trigger an error of type E_NOTICE during comparison, but not when using empty. You can work around this using the isset call before your comparison, like this:
if(isset($foo) && $foo != '')
if()
"converts" the statement given to a bool, so taking a look at the documentation for boolean seems to be what you're looking for. in general:
""
), empty arrays ( array()
), zero ( 0
) and boolean false ( false
) are treated as false
"foo"
, 1
, array('foo')
, true
, ...) is treated as true
EDIT :
for more information, you could also check the type comparison tables .
empty($foo) should return true in all of these cases: 0,"", NULL.
For a more complete list check this page: http://php.net/manual/en/function.empty.php
No, it's not equal. When variable is not defined, expression without empty
will generate notice about non-defined variable.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.