Why does a person want to use isset only through! $ VariableName

I was wondering why someone wants to use the isset function only through ! to say that the variable is not set or does not exist.

For instance:

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

Won't they be repeated? I was told it is better to use isset , but I do not understand why. Can someone explain?

+4
source share
4 answers

if we use directly $ variableName, and the variable $ variableName is not defined before this check, then php will generate an Undefined warning. If we use isset, then if the variable is defined earlier, then it is established that isset will return true, if not defined, then setset will return false, and therefore, the warning Undefined Variable or index will not be generated.

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

 <?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'; } ?> 
+2
source

If the variable does not exist and you are trying to get it, this will result in a warning:

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

The value of this non-existent variable is null , so the result will be the same, which means that the if condition will not be fulfilled and the code will not be executed, but you will receive this warning informing you of a potential problem (what it is).

You use isset to check if a variable exists without triggering an error. You should only use this for variables that you really cannot be sure of. If a variable must be set at any point in your code, you do not need isset ; instead, you want a warning if the variable does not exist, when it should.

+1
source

Not set values

If $name not defined, the first if generates a warning and returns false . ( E_NOTICE )

The second if will function normally and will return false .

Boolean values

If $name is true , then only the second if will print echo .

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

Thus, isset more consistent with what you want to extract from this conditional. You want to know if the value of $name value, defined or not.

Integer values

if $name is 0 , the first if will print that name, but the second if it returns true , because the $name variable is set. Thus, more coherence.

Conclusion

isset better because it does exactly what you want and what you want.

+1
source

Function isset() Determines whether a variable is set and is not NULL.

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

If multiple parameters are specified, then isset() will return TRUE only if all parameters are set. The evaluation goes from left to right and stops as soon as an undefined variable is encountered.

If you are not using isset() , the script will generate a warning, with isset() you can prevent this.

a For example, the reason for using isset() is to detect if the key is inside an array.

eg:

 <?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 NOT , therefore, if you get the same result in the if if line, but the first line will display an Undefined variable, because there is no $name variable, the second if you do NOT before $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'; } ?> 
0
source

Source: https://habr.com/ru/post/1501834/


All Articles