lundi 21 janvier 2019

PHP - Which conditional test to use?

Are those 2 expressions equivalent (I mean "can I replace the 1st one with the 2nd one):

if ($var) { ... }

and

if (!empty($var)) { ... }

I feel there is a difference but I rationally cannot say which one.

For me the first one evaluates if $var is true or false and I may be wrong but "false" evaluations means that $var is false (boolean), empty (string, object or array), 0 value (int, float or string) or undefined ... that's the way the "empty" function works (http://php.net/manual/en/function.empty.php).

If those tests are equivalent (at least in specific cases), which is better to use (readability, performance, maintenance, ...)?

Thanks

Aucun commentaire:

Enregistrer un commentaire