An example of Isset () function variable in PHP

Source: Internet
Author: User
Tags constant mixed parse error set set strlen

The Isset function is to detect whether a variable is set.

Format: bool Isset (mixed var [, mixed Var [, ...]])

return value:

Returns FALSE if the variable does not exist
Returns FALSE if the variable exists and its value is null
Returns ture if the variable exists and the value is not NULL
When you check multiple variables at the same time, each item returns TRUE when it meets the previous requirement, otherwise the result is FALSE
If you have freed a variable using unset (), it will no longer be isset (). If you use Isset () to test a variable that is set to NULL, it returns FALSE. Also note that a null byte ("") is not equivalent to PHP's null constant.


To get user properties, some users can, some users cannot, the same logic why can not be checked using code like this

The code is as follows Copy Code

$userInfo = ' abc ';
if (Isset ($userInfo [' account '])) {
$account = $userInfo [' account '];
} else {
$account = $userInfo;
}

Found $account was empty, checked some data, and didn't get the results.
Did an experiment to get the conclusion

The code is as follows Copy Code

$a = ' abc ';
echo $a [' account '];

The result is a.

When the string key is not there, it translates into a number key, which is 0, so it gets a
Now the php5.4 has been repaired.

Take this opportunity to tidy up the use of isset, for everyone to learn from

The code is as follows Copy Code

<?php

$var = ';

if (Isset ($var)) {
Print "This var is set set and so I'll print.";
}

In the example below, we will use the Var_dump function to output the return value of Isset ().

$a = "Test";
$b = "Anothertest";

Var_dump (Isset ($a)); TRUE
Var_dump (Isset ($a, $b)); TRUE

unset ($a);

Var_dump (Isset ($a)); FALSE
Var_dump (Isset ($a, $b)); FALSE

$foo = NULL;
Var_dump (Isset ($foo)); FALSE

?>

This is also valid for elements in the array:

The code is as follows Copy Code

<?php

$a = array (' Test ' => 1, ' hello ' => NULL);

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

' Hello ' equals NULL, so it is considered unassigned.
If you want to detect NULL key values, try the bottom method.
Var_dump (array_key_exists (' hello ', $a)); TRUE

?>

The value of the key ' B ' is equal to NULL, so it is considered an open value.
But the value of the key ' C ' is null. The result is true, and the null value is set. If you want to detect NULL key values, try the bottom method.

The code is as follows Copy Code

Var_dump (array_key_exists (' hello ', $a)); TRUE

Warning: isset () can only be used for variables because passing any other parameter will result in a parse error. To detect if a constant is set, use the defined () function.

Some examples of applications


PHP isset () for PHP parameters you can use the PHP isset () to determine whether a parameter is defined, note that if the parameter is empty, or "n" (null byte) after using PHP isset () to judge, will be true.

The code is as follows Copy Code

<?php
$weigeti = '; Here the initial parameter $weigeti= ';
$weigeti 0=false//Here The initial parameter is Boolean FALSE
$weigeti 2=null; Here defines $weigeti2=null, returns false with Isset () judgement
$weigeti 3 = ""; Here is a null byte, judged by Isset (), returns True, different from the null constant
$weigeti 4= "www.v-get.com"; The definition here will be unset down.

if (Isset ($weigeti)) {echo ' parameter $weigeti is set and the value is not NULL ';}
Parameter $weigeti has been assigned and the value is $weigeti= ', so the parameter $weigeti has been set.

if (Isset ($weigeti 0)) {echo ' parameter $weigeti0 has been set and the value is not NULL ';}
Parameter $weigeti0 has been assigned and the value is $weigeti=false, so the parameter $weigeti is set.

if (Isset ($vget)) {echo ' parameter $vget is set and the value is not NULL ';}
Parameter $vget is not mentioned at all, Isset ($vget) returns false and does not output

if (Isset ($weigeti 2)) {echo ' parameter $weigeti2 has been set and the value is not NULL ';}
This is $WEIGETI2, although it is set, but $weigeti2=null, so Isset ($weigeti 2) returns false and does not output

if (Isset ($weigeti 3)) {echo ' parameter $weigeti3 has been set and the value is not NULL ';}
Parameter $weigeti3, although it represents a null byte, is not a constant null, so $isset ("") returns True, outputting

Unset ($weigeti 4); It's released to $weigeti4.
if (Isset ($weigeti 4)) {echo ' parameter $weigeti4 has been set and the value is not NULL ';}
Although the beginning has been defined $weigeti4, and is not NULL, but then to unset () dropped, so will not output.
?>

PHP isset () in the use of PHP arrays, you need to determine whether a value of the array exists or use the PHP isset () function.

The code is as follows Copy Code

<?php
$V =array (
' Weigeti ' => ' www.111cn.net ',
' V-get ' =>array (
    ' www ' => ') Www.v-get.com ',
    ' e ' => ' e.v-get.com ',
    ' Wuliu ' =>array (
         ' www ' => ' wuliu.v-get.com ',
        ' Yiwu ' => ' www.111cn.net '
    )
  )
);

//Var_dump for output true or FALSE


Var_dump (isset ($V [' Weigeti ']);
//array $v $v[' weigeti '] = ' www.111cn.net ' already exists, so output true

Var_dump ( Isset ($V [' v-get '] [' e '])];
//$V [' v-get '] [' e ']= ' e.v-get.com ' so outputs TRUE

Var_dump (Isset ($V [' v-get '] [' Wuliu '] [' Yiwu ']);
//$V [' v-get '] [' Wuliu '] [' Yiwu ']= ' www.111cn.net ' so outputs true

Var_dump (Isset ($V [' v-get '] [' Wuliu '] [' sh ']]);
//Based on array, the item is not set at all, so output is false

PHP isset () multi-parameter judgement many times we need to judge multiple parameters, we can use Isset ($A) &&isset ($B) ... To determine if all of these parameters have been set and are NOT NULL, you can use Isset () multiple parameters to determine if it is all set.

The code is as follows Copy Code
<?php
Isset ($v 1) &&isset ($v 2) &&isset ($v 3) ...
Equivalent to
Isset ($v 1, $v 2, $v 3 ...)
?>

Using Isset () to determine multiple parameters, all parameters are set and are not NULL, so that the entire isset () is false as long as one of the parameters is not set or null.

PHP isset () judges $_post, $_get, $_request equivalents. Using Isset () or empty () to determine the parameters passed through the form is the most common use of isset ().

The code is as follows Copy Code
if (Isset ($_post[' from ']) && ' e.v-get.com ' ==$_post[' from ') {
The source of the Echo ' website via post is e.v-get.com ';
}

According to the above we can use Isset to judge multiple parameters, in the form passed the value of the most convenient to judge.

The code is as follows Copy Code

<?php
Form delivery The user name and pass must exist at the same time to perform
if (Isset ($_post[' user '],$_post[' pass ')) {
$user =$_post[' user '];
$pass =$_post[' Pass '];
Echo ' The username you fill out is: ', $user, '; The password is: ', $pass;

}
?>

PHP isset ($var {string length}) to determine the length of a string in PHP to determine whether a string exists or to determine whether the string length exceeds a number or whether the string length is null general use strlen (), but in fact, the use of isset () performance is superior.

  code is as follows copy code

<?php
//ternary, equivalent to if (Isset ($_post[' from ')) {$weigeti =$_post[' from '];} else{$weiget = ';}

$weigeti =isset ($_post[' from ') "$_post[' from ']:";

//Judge $weigeti exists with string length greater than 0
/* Rookie writing  
if (strlen ($weigeti) >0) {
echo ' $weigeti substring longer than 0 ';}
*/

//Better performance to determine whether the $weigeti 1th character is set
if (Isset ($weigeti {1})) {
echo ' $weigeti character length greater than 0 ';
}
?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.