If ($ val) vs. if ($ val! = "") versus if (! empty ($ val)) - which one?

I see that many people use different methods to check if a variable is empty, in fact, there seems to be no consensus. I heard that if($foo) exactly the same as if(!empty($foo)) or if($foo != "") . It's true?

I understand that this is a very simple question, but I would really like to know. Are there any differences? Which method should be used?

+8
php if-statement
source share
6 answers

The difference between the naked test and the comparison with the empty string

if($foo != "") in most cases equivalent to if($foo) , but not always.

To find out where the differences are, consider the behavior of the comparison operator and conversion to string rules for the first case and conversion to logical rules for the second case.

I learned that:

  • if $foo === array() , the if($foo != "") test will succeed (arrays are greater than "), but the if($foo) test will fail (empty arrays are converted to boolean false )
  • if $foo === "0" (string), if($foo != "") test will succeed again (obviously), but if($foo) test will fail (string "0" converted to boolean false )
  • If $foo is a SimpleXML object created from an empty tag, the if($foo != "") Test will succeed again (more than objects), but the if($foo) test will fail (such objects are converted to boolean false )

See the differences in action .

Best way to test

the preferred testing method is if(!empty($foo)) , which does not quite match the above:

  • It does not suffer from inconsistencies if($foo != "") (Which IMHO is just awful).
  • It will not generate E_NOTICE if $foo not in the current area, which is its main advantage over if($foo) .

There is a caveat: if $foo === '0' (string of length 1), then empty($foo) will return true , which usually (but may not always) be what you want. This also applies to if($foo) though.

Sometimes you need to test with an identical statement

Finally, an exception to the above should be made when there is a certain type of value that you want to check. As an example, strpos may return 0 , and may also return false . Both of these values ​​will fail the test if(strpos(...)) , but they have completely different values. In these cases, a test with an identical operator is performed as follows: if(strpos() === false) .

+16
source share

No, this is not always the case. When you do if($foo) , PHP translates the variable into Boolean. An empty string, an integer of zero, or an empty array will be false . This can sometimes be a problem.

You should always try to use the most accurate comparison if you expect a string that may be empty to use if($foo==='') (note the three equal signs). If you expect either a (logical) value of false or a resource (for example, from a database query), use if($foo===false){...} else {...}

+4
source share

You can read the boolean casting documentation to find the answer to this question. There is a list in which the values ​​are converted to true and false respectively.

Note that empty also checks to see if a variable that does not have a normal comparison is set. The unset variable causes an error of type E_NOTICE during comparison, but not when using empty. You can get around this using the isset call before comparing, for example:

 if(isset($foo) && $foo != '') 
0
source share

if() "converts" the statement given to bool, so I seem to be looking for a look at the documentation for the boolean . generally:

  • empty strings ( "" ), empty arrays ( array() ), zero ( 0 ) and the boolean false ( false ) are considered false
  • everything else ( "foo" , 1 , array('foo') , true , ...) is considered true

EDIT:
for more information, you can also check type comparison tables .

0
source share

empty ($ foo) should return true in all of these cases: 0, "", NULL.

For a more complete list check out this page: http://php.net/manual/en/function.empty.php

0
source share

No, he is not equal. If the variable is not defined, an expression without empty will generate a notification of the undefined variable.

0
source share

All Articles