The !
character is the logical "not" operator. It inverts the boolean meaning of the expression.
If you have an expression that evaluates to TRUE
, prefixing it with !
causes it evaluate to FALSE
and vice-versa.
$test = 'value';
var_dump(isset($test)); // TRUE
var_dump(!isset($test)); // FALSE
isset()
returns TRUE
if the given variable is defined in the current scope with a non-null value.
empty()
returns TRUE
if the given variable is not defined in the current scope, or if it is defined with a value that is considered "empty". These values are:
NULL // NULL value
0 // Integer/float zero
'' // Empty string
'0' // String '0'
FALSE // Boolean FALSE
array() // empty array
Depending PHP version, an object with no properties may also be considered empty.
The upshot of this is that isset()
and empty()
almost compliment each other (they return the opposite results) but not quite, as empty()
performs an additional check on the value of the variable, isset()
simply checks whether it is defined.
Consider the following example:
var_dump(isset($test)); // FALSE
var_dump(empty($test)); // TRUE
$test = '';
var_dump(isset($test)); // TRUE
var_dump(empty($test)); // TRUE
$test = 'value';
var_dump(isset($test)); // TRUE
var_dump(empty($test)); // FALSE