PHP Functions: Empty () and isset () are the functions that determine whether a variable has been configured, but there are certain differences when used.
The empty () function is used to test whether a variable has been configured. Returns a value of False if the variable already exists, is not an empty string, or is Non-zero, otherwise returns true.
The Isset () function is used to test whether a variable has been configured. Returns a true value if the variable already exists. Other cases return a value of false.
From the definitions of the above two functions, you can see that empty () and isset () have the common denominator that all can determine whether a variable is empty and return a Boolean type, that is, true or false. The most obvious difference is that the returned Boolean is exactly the opposite.
In addition, the biggest difference between them is that for 0 of the judgment, if using empty judgment will be considered empty, with isset is considered not empty, for example:
<?php
var $a = 0;
Empty ($a) return true
if (empty ($a)) {
echo "Judged the result to be empty"
}
Isset ($a) return true
if (Isset ($a)) {
echo "Judge the result is not empty"
}
?>
Empty () Determines whether a variable is "empty" and also detects whether the variable is empty and zero. When a variable value is 0,empty (), the variable equals null, which is equivalent to no setting. and Isset () to determine whether a variable has been set, even if the variable value is NULL, is also set to zero.
One, the example explains
A. How do you determine whether a variable is defined?
<?php
Assuming there is no $test variable
$isset = Isset ($test)? ' Test is define! ': ' Test is undefine! ';
echo "Isset: $isset", ' <br/> ';
$empty =!empty ($test)? ' Test is define! ': ' Test is undefine! ';
echo "Empty: $empty", ' <br/> ';
$is _null = Is_null ($test)? ' Test is define! ': ' Test is undefine! ';
echo "Is_null: $is _null";
The test results are:
Isset:test is undefine!
Empty:test is undefine!
Is_null:test is define!
The results show that Empty,isset first checks whether the variable exists, and then detects the value of the variable. The Is_null just checks the value of the variable directly and is null, so if the variable is undefined, an error occurs!
B, to see what the parameters are received?
isset function Parameters:
<?php
$test = 100;
echo isset ($test), Isset (M), $isset ($b =100);
Output:
<br/>
<b>parse Error</b>: Parse error, unexpected T_lnumber, expecting t_string or t_variable or ' $ ' in <b>ph pdocument3</b> on line <b>3</b><br/>
Empty function Parameters:
<?php
$test = 100;
echo Empty ($test), Empty (m), empty ($b =100);
Output:
<br/>
<b>parse Error</b>: Parse error, unexpected T_lnumber, expecting t_string or t_variable or ' $ ' in <b>ph pdocument3</b> on line <b>3</b><br/>
is_null function Parameters:
<?php
$test = 100;
echo Is_null ($test), Is_null (M), Is_null ($b =100);
Run Result: there are no errors.
The results show that the Empty,isset input parameter must be a variable (PHP variable begins with a $ character), and the Is_null input parameter can (constants, variables, expressions, etc.) as long as it can have a return value. In the PHP manual, for them parsing is: Empty,isset is a language structure rather than a function, so it cannot be called by the variable function.
Two, Summary summary isset,empty,is_null difference:
Empty ()
If the variable is non-null or Non-zero, empty () returns FALSE. In other words, "", 0, "0", NULL, FALSE, Array (), Var $var, undefined, and objects without any attributes will be considered empty and TRUE if Var is empty.
Isset ()
Returns TRUE if the variable exists and the value is not NULL, or FALSE (including undefined). Variable value is set to: null, the return is also false;unset a variable, the variable was canceled. Note that isset for null value variables, special handling.
Is_null ()
Detects whether the passed-in value "value, variable, expression" is null, has only one variable defined, and its value is NULL, it returns TRUE. All other returns FALSE "undefined variable passed in error!" 】。
1, Isset () to the PHP parameter judgment
You can use PHP isset () to determine whether a parameter is defined, note that if the argument is empty, or if "\ n" (null byte) is judged using PHP isset (), it will be true.
<?php
$var = '; Here the initial parameter $var= ';
$var 0 = false//Here the initial parameter is Boolean FALSE
$var 2 = NULL; Here defines $var2=null, returns false with Isset () judgement
$var 3 = "the"; Here is a null byte, judged by Isset (), returns True, different from the null constant
$var 4 = "www.qianyunlai.com"; The definition here will be unset down.
if (Isset ($var)) {
echo ' parameter $var has been set and the value is not NULL ';
}
Parameter $var has been assigned and the value is $var= ', so the parameter $var has been set.
if (Isset ($var 0)) {
echo ' parameter $var0 has been set and the value is not NULL ';
}
Parameter $var0 has been assigned and the value is $var=false, so the parameter $var is set.
if (Isset ($vget)) {
echo ' parameter $vget has been set and the value is not NULL ';
}
Parameter $vget is not mentioned at all, Isset ($vget) returns false and does not output
if (Isset ($var 2)) {
echo ' parameter $var2 has been set and the value is not NULL ';
}
This is $var2, although it is set, but $var2=null, so Isset ($var 2) returns false and does not output
if (Isset ($var 3)) {
echo ' parameter $var3 has been set and the value is not NULL ';
}
Parameter $var3 Although it represents a null byte, this is not a constant null, so $isset ("") returns True, outputting
Unset ($var 4); It's released to $VAR4.
if (Isset ($var 4)) {
echo ' parameter $VAR4 has been set and the value is not NULL ';
}
Although the beginning has been defined $VAR4, and is not NULL, but then to unset () dropped, so will not output.
?>
2, PHP isset () The evaluation of the array
When using an array of PHP, you need to determine whether a value of the array exists or use the PHP isset () function.
<?php
$V => Array (
' AA ' => ' www.qianyunlai.com ',
' BB ' => array (
' File ' => ' file.qianyunlai.com ',
' img ' => ' img.qianyunlai.com ',
' Hosts ' => Array (
' Blog ' => ' blog.qianyunlai.com ',
' Res ' => ' res.qianyunlai.com '
)
)
);
Var_dump is used to output true or FALSE
Var_dump (Isset ($V [' AA ']));
Array $v $v[' aa ' = ' www.qianyunlai.com ' already exists, so output true
Var_dump (Isset ($V [' BB '] [' file ']);
$V [' BB '] [' file '] = ' file.qianyunlai.com ' so output TRUE
Var_dump (Isset ($V [' BB '] [' hosts '] [' blogs ']);
$V [' BB '] [' hosts '] [' blog ']= ' blog.qianyunlai.com ' so output true
Var_dump (Isset ($V [' BB '] [' hosts '] [' sh ']);
The item is not set at all based on the array, so the output is false
?>
3, PHP isset () to determine the parameters
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.
<?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.
4, PHP isset () Judge $_post, $_get, $_request equivalent
Using Isset () or empty () to determine the parameters passed through the form is the most common use of isset ().
if (Isset ($_post[' from ']) && ' qianyunlai.com ' = = $_post[' from ']) {
The source of the Echo ' website via post is qianyunlai.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.
<?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;
}
?>
5, PHP Isset ($var {string length}) to determine the length of the string
Using PHP to determine whether a string exists or to determine whether the string is longer than a certain number or whether the string length is null generally uses strlen (), but in fact, using isset () performance is superior.
<?php
Ternary, equivalent to if (Isset ($_post[' from ')) {$var = $_post[' from '];} else {$weiget = ';}
$var = Isset ($_post[' from '])? $_post[' from ']: ';
The $var is judged to exist, and the string length is greater than 0
* * Rookie Wording
if (strlen ($var) > 0) {
echo ' $var substring length greater than 0 ';
}
*/
Better performance of the writing to determine whether the $var 1th character is set
if (Isset ($var {1})) {
Echo ' $var character length greater than 0 ';
}
?>
6, PHP isset () and PHP empty () the difference
A lot of people are comparing PHP isset () with PHP empty (), but the conclusion is generally PHP isset () to determine whether this parameter exists, and is not NULL, that is, using PHP isset () to determine the null value, 0, Boolean (true/false) are TR UE, and PHP isset () can also be judged on an array.
and PHP empty (), if the variable does not exist, or the variable exists and its value is "", 0, "0", NULL, FALSE, Array (), Var $var; and an object without any attributes, returns ture.
Then!empty (), the variable exists, and the value is not "", 0, "0", NULL, FALSE, Array (), and Var $var;
<?php
$_post[' from ' is a URL that we can know that this value must not be 0 and Boolean, only a string, or an empty string, or no
/* The following wording is more common, but need to judge two times
if (Isset ($_post[' from ')) && isset ($_post[' from ']{1})) {
echo ' $weigeti substring length greater than 0 ';
}
*/
The following with!empty () only need to judge once, more efficient
if (!empty ($_post[' from ')) {
Echo ' transmits from existence and is not NULL, 0 etc. ';
}
?>
Although sometimes using!empty () to judge the passing of $_get, $_post performance is better, but not completely replace isset () use, if the value passed to allow the inclusion of 0, ' 0 ' or empty, you can not use the!empty () judgment, or even if passed over 0, ' 0 ' will be treated as not passing any value.
And for can be sure that the value passed over must not be 0, ' 0 ', empty time, such as user name, password, etc., using!empty () performance is superior.
Add: PHP Determines whether constants, variables, and functions exist
if (defined (' Const_name ')) {
Do something
}
Variable detection is the use of isset, note that the variable is not declared or declared when the assignment is Null,isset return false, such as:
if (Isset ($var _name)) {
Do something
}
function detection with function_exists, note that the function name to be detected also need to use quotes, such as:
if (function_exists (' Fun_name ')) {
Fun_name ();
}
<?php
/* To determine whether a constant exists/*
if (defined (' myconstant ')) {
Echo myconstant;
}
To determine whether a variable exists
if (Isset ($myvar)) {
echo "exists variable $myvar."
}
To determine whether a function exists
if (function_exists (' Imap_open ')) {
echo "exists function Imag_openn";
} else {
echo "function Imag_open does not exist n";
}
?>
Function_exists to determine whether a function exists
<?php
if (function_exists (' Test_func ')) {
echo "function test_func exist";
} else {
echo "function Test_func does not exist";
}
?>
Filter_has_var function
The Filter_has_var () function checks for the existence of a variable of the specified input type.
If successful, returns TRUE, otherwise returns false.
<?php
if (!filter_has_var (Input_get, "name"))
{
Echo ("Input type does not exist");
}
Else
{
Echo ("Input type exists");
}
?>
Output is. Input type exists
Other variable processing functions:
GetType: Gets the type of the variable.
Intval: variable is converted to an integer type.
Doubleval: Variable multiplies floating-point type.
Empty: Determines whether the variable is configured.
Is_array: Determines whether the variable type is an array type.
Is_double: Determines whether the variable type is a floating-point number type.
Is_float: Determines whether the variable type is a floating-point type.
Is_int: Determines whether the variable type is an integer type.
Is_integer: Determines whether the variable type is a long integer type.
Is_long: Determines whether the variable type is a long integer type.
Is_object: Determines whether the variable type is a class type.
Is_real: Determines whether the variable type is a real type.
Is_string: Determines whether the variable type is a string type.
Isset: Determines whether the variable is configured.
Settype: Configuration variable type.
Strval: Converts a variable to a string type.
unset: Deletes a variable.