I
I
iv4n0v0072018-02-27 13:22:40
PHP
iv4n0v007, 2018-02-27 13:22:40

What is the difference between empty() and !?

Which method is better to use for checking if a variable is empty:
if ( empty($foo) )or if ( !$foo )
And similarly, checking if a variable is not empty:
if ( !empty($foo) )or just if ( $foo )
As far as I know, using empty and just ! before the variable, they check the same thing (check for an empty string / 0 / null / false / empty array), i.e. the result is the same, but what is more relevant to use on php 7.1?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Evgeny Kalibrov, 2018-02-27
@iv4n0v007

You are wrong, they work differently. Here you can study the table how different check methods work empty, is_null, isset, boolean, if($x) - php.net/manual/en/types.comparisons.php
What to use should be based on the logic of your code and this table.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question