3

Possible Duplicate:
Reference - What does this symbol mean in PHP?
Getting confused with empty, isset, !empty, !isset

In PHP what is the difference between:

if(!isset) 
if(isset)

Same with if(!empty) and if(empty)?

What does the "!" character mean?

Community
  • 1
  • 1
oscar
  • 111
  • 2
  • 3
  • 9
  • 1
    `if( isset($test) === false )` and `if( !isset($test) )` are equivalent. – MetalFrog Apr 03 '12 at 13:47
  • 4
    Suggestion: Read the docs on [empty](http://us.php.net/manual/en/function.empty.php) and [isset](http://us.php.net/manual/en/function.isset.php) to get a sense for how they work, differences, etc. – Brian Driscoll Apr 03 '12 at 13:48
  • @jprofitt: actually, the logical negation operator appears to be missing from that list. You could probably add this question as the reference on `!`. – Li-aung Yip Apr 03 '12 at 13:56

3 Answers3

20

! is the logical negation or NOT operator. It reverses the sense of the logical test.

That is:

  • if(isset) makes something happen if isset is logical True.
  • if(!isset) makes something happen if isset is logical False.

More about operators (logical and other types) in the PHP documentation. Look up ! there to cement your understanding of what it does. While you're there, also look up the other logical operators:

  • && logical AND
  • || logical OR
  • xor logical EXCLUSIVE-OR

Which are also commonly used in logic statements.

Li-aung Yip
  • 12,320
  • 5
  • 34
  • 49
8

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
DaveRandom
  • 87,921
  • 11
  • 154
  • 174
  • String `'0'` evaluates to logical false in PHP? Ugh. :( – Li-aung Yip Apr 03 '12 at 13:53
  • @Li-aungYip I know it seems illogical but it is useful. Because PHP is *very* loosely typed, it makes sense when you consider that one is often working with data from `$_POST` and friends, where all data is `(string)`. In this instance, it is useful to be able to pass integers as booleans, and in order for this to work without explicit casting, `'0'` must evaluate to `FALSE`. Where it gets confusing is the fact that casting a string to int usually results in `0`, yet casting as bool results in `TRUE`, so `(bool) $str != (bool) (int) $str` – DaveRandom Apr 03 '12 at 13:56
  • It's a useful idiom because it's easy - but type coercion always makes me a bit queasy because of the potential bugs. A typical pattern for me is to test the emptiness of a string by asking `if(string)`. In PHP both the empty string `''` and the literal `'0'` would pass this test, so I'd have to unlearn that habit. – Li-aung Yip Apr 03 '12 at 14:00
  • Thanks, the example made a lot of sense – oscar Apr 03 '12 at 14:02
  • At least [PHP isn't Javascript](https://www.destroyallsoftware.com/talks/wat). Wat. ([See explanations on SO.](http://stackoverflow.com/questions/9032856/what-is-the-explanation-for-these-bizarre-javascript-behaviours-mentioned-in-the)) – Li-aung Yip Apr 03 '12 at 14:02
  • @Li-aungYip You see I am a PHP native so I am already is the habit of `===`ing everything, and as result I never run into those interesting little ECMA oddities you pointed either - but I can see how it would be annoying for those coming from a strongly typed background. – DaveRandom Apr 03 '12 at 14:06
3
$var = 0;

// Evaluates to true because $var is empty
if (empty($var)) {
    echo '$var is either 0, empty, or not set at all';
}

// Evaluates as true because $var is set
if (isset($var)) {
    echo '$var is set even though it is empty';
}

Edit:

here is a test case for you:

$p = false;
echo isset($p) ? '$p is setted : ' : '$p is not setted : ';
echo empty($p) ? '$p is empty' : '$p is not empty';
echo "<BR>";

$p is setted : $p is empty

Churk
  • 4,556
  • 5
  • 22
  • 37