PHP: When To Use empty()

empty($var) is equivalent to !isset($var) || !$var and inherently
!empty($var) is equivalent to isset($var) && $var.

empty() should be used when you are not sure if the variable exists. As variables most oftenly do exist, empty() should be only rarely used. Instead, the logical NOT (!) operator should be used to test the value of the variable.

Some programmers might still prefer to use empty() for readability purposes:
if (empty($var)) { } might be more readable than if (!$var) { }

Post a Comment