3

I was wondering why would someone want to use the function isset over just the ! to say a variable isn't set or existant.

For example:

<?php
    if(!$name){
        echo 'Name is not set';
    }
    if(isset($name) == false){
        echo 'Returned false';
    }
?>

Wouldn't they both be echoed out? I've been told that using isset is better but I fail to see why. Can someone explain?

Jake
  • 1,469
  • 4
  • 19
  • 40
  • Because otherwise error reporting is useless → duplicate of [Why should I fix E\_NOTICE errors?](http://stackoverflow.com/questions/5073642/why-should-i-fix-e-notice-errors), [The Definitive Guide To PHP's isset And empty](http://kunststube.net/isset/) – deceze Sep 12 '13 at 12:02
  • 1
    You want to use `(!$variableName)` but you still use `(isset($name) == false)`? :D `(!isset($name))` will save your time, because it only returns boolean values. – sybear Sep 12 '13 at 12:07
  • I wasn't asking about any errors, I was asking about why one is better than the other. So not a duplicate. And thank you Jari, I knew you could do that but kind of spaced out when writing this. – Jake Sep 12 '13 at 12:39
  • The reason *is* errors. The question is phrased differently, yes, but the core issue is errors. – deceze Sep 12 '13 at 12:45

4 Answers4

3

if we use directly $variableName and the $variableName is not defined before that check, then a Undefined variable warning will be generated by php. If we use isset, then if the variable is defined before, then it is set the isset will return true, if not defined then isset will return false, and hence no Undefined Variable or index warning will be generated.

It is a good practice to use isset with variables if there is a case that it is not defined before.

<?php
    if(!$name){   //this will generate a warning
        echo 'Name is not set';
    }
    if(isset($name) == false){   //this will not as isset returns false
        echo 'Returned false';
    }
?>
Altaf Hussain
  • 5,166
  • 4
  • 30
  • 47
  • The operator `!` in PHP use as `NOT` function and not to use to check if variable is set! Your answer is "what will happens" if he use it like this, you not explained why this happened. – One Man Crew Sep 12 '13 at 12:26
  • Yes, ! is logical NOT operator, and it convert TRUE to FALSE. Here 1 is true, 2 is also and "you are a genius" is also true. On whatever data use use ! , if it is not null and defined before using !, it will work fine. But if the variable or data is not defined, it will still work but php will generate a warning also. – Altaf Hussain Sep 12 '13 at 13:13
1

If a variable does not exist and you're trying to access it, this will generate a warning:

if ($foo) ... // E_NOTICE: Undefined variable foo

The value of this non-existent variable is null so the result is the same, meaning that the if condition will not be satisfied and the code will not execute, but you get this warning notifying you of a potential problem (which it is).

You use isset to test whether a variable exist without triggering an error. You should only use this for variables which you really cannot be sure about. If a variable should be set at some point in your code, you do not need isset; you want a warning notice instead if a variable doesn't exist when it should.

deceze
  • 510,633
  • 85
  • 743
  • 889
1

Not defined values

If $name is not defined the first if will output a warning and return false. ( E_NOTICE )

The second if will function normally and will return false.

Boolean values

If $name is true only the second if will output the echo.

if(!$name){
    echo 'Name is not set';
}

if(isset($name) == false){
    echo 'Returned false';
}

Thus, isset is more coherent with what you want to retrieve from this conditional. You only want to see if $name has a value defined or not.

Integer values

if $nameis 0 the first if will output that name is not set, but the second if will return true, because the variable $name is set. Thus more coherency.

Conclusion

isset is better because it does exactly what you want to retrieve and what you want.

Tomás
  • 3,501
  • 3
  • 21
  • 38
-1

The isset() function Determine if a variable is set and is not NULL.

If a variable has been unset with unset(), it will no longer be set. isset() will return FALSE if testing a variable that has been set to NULL. Also note that a NULL byte ("\0") is not equivalent to the PHP NULL constant.

If multiple parameters are supplied then isset() will return TRUE only if all of the parameters are set. Evaluation goes from left to right and stops as soon as an unset variable is encountered.

if you not using isset() the script will generated a warning, with isset() you can prevents this.

a for example reason to use isset() is to detect if key is inside array.

for example:

<?php

$a = array ('test' => 1, 'hello' => NULL, 'pie' => array('a' => 'apple'));

var_dump(isset($a['test']));            // TRUE
var_dump(isset($a['foo']));             // FALSE
var_dump(isset($a['hello']));           // FALSE

// The key 'hello' equals NULL so is considered unset
// If you want to check for NULL key values then try: 
var_dump(array_key_exists('hello', $a)); // TRUE

// Checking deeper array values
var_dump(isset($a['pie']['a']));        // TRUE
var_dump(isset($a['pie']['b']));        // FALSE
var_dump(isset($a['cake']['a']['b']));  // FALSE

 if(!$a['hello']){   //this will display a warning: Undefined variable.
    echo 'hello is not set in the array';
}
?>

The problem with your example is the operator ! in PHP is NOT, So in the two if line's here you will get the same result but the first line will display Undefined variable warning because there is no $name variable, the second if will do NOT to $name variable

<?php
    if(!$name){//this will display a warning: Undefined variable.
        echo 'Name is not set';
    }

     $name=0;
     if(!$name){//check the not value of $name.
        echo 'Name is true';
    }

?>
One Man Crew
  • 9,420
  • 2
  • 42
  • 51