Home > Article > Backend Development > Introduction to the difference between PHP isset() and empty() functions_PHP tutorial
PHP’s isset() function is generally used to detect whether a variable is set
Format: bool isset ( mixed var [, mixed var [, ...]] )
Function: detect whether a variable is set
Return value:
If the variable does not exist, it returns FALSE
If the variable exists and its value is NULL, it also returns FALSE
If the variable exists and its value is not NULL, it returns TRUE
At the same time When checking multiple variables, TRUE will be returned only when each single item meets the previous requirement, otherwise the result is FALSE
Version: PHP 3, PHP 4, PHP 5
More instructions:
Use unset() After the variable is released, it is no longer isset().
The PHP function isset() can only be used for variables. Passing any other parameters will cause a parsing error.
To check whether a constant has been set, use the defined() function.
PHP's empty() function determines whether the value is empty
Format: bool empty (mixed var)
Function: Check whether a variable is empty
Return Value:
Returns TRUE if the variable does not exist
If the variable exists and its value is "", 0, "0", NULL,, FALSE, array(), var $var; and objects without any attributes , then return TURE
If the variable exists and the value is not "", 0, "0", NULL,, FALSE, array(), var $var; and an object without any attributes, then return FALSE
version : PHP 3, PHP 4, PHP 5
More explanation:
The return value of empty() =! (boolean) var, but no warning message will be generated because the variable is undefined. See Converting to Boolean for more information.
empty() can only be used for variables. Passing any other parameters will cause a Paser error and terminate the operation.
To check whether a constant has been set, use the defined() function.
Example: A simple comparison between empty() and isset()
The code is as follows:
$var = 0;
// The result is true because $var is empty
if (empty($var)) {
echo $var is either 0 or not set at all;
}
// The result is false because $var has been set
if (!isset($var)) {
echo $var is not set at all;
}
?>
Note: Since this is a language structure rather than a function, it cannot be Variable function call.
Note: empty() only detects variables, detecting anything that is not a variable will result in a parsing error. In other words, the following statement will not work: empty(addslashes($name)).
The following is a detailed example code of isset and empty functions that has been tested by Script House. After reading this, it is basically the same:
Copy the code as follows:
error_reporting(E_ALL);
echo undefined $var
;
echo "isset test:
";
if ( isset ( $ var ))
{
echo variable $var exists!
;
}
echo "empty test:
";
if ( empty ( $var )) {
echo The value of the variable $var is empty
;
}
else
{
echo The value of the variable $var is not empty
;
}
echo "Direct variable test:
";
if ( $var ){
echo variable $var exists!
;
}
else {
echo The variable $var does not exist!
;
}
echo --------------------------------- ----
;
echo $var =
;
echo "isset test:
";
$var = ;
if ( isset ( $var ))
{
echo variable $var exists!
;
}
echo "empty test:
";
if ( empty ( $var )){
echo The value of variable $var is empty
;
}
else
{
echo The value of variable $var is not empty
;
}
echo "Direct variable test:
";
if ( $var ){
echo variable $var exists!
;
}
else {
echo variable $var does not exist!
;
}
echo --------------------- -------------
;
echo $var = 0
;
echo isset test:
;
$var = 0 ;
if ( isset ( $var ))
{
echo variable $var exists!
;
}
echo "empty test:
";
if ( empty ( $var )){
echo The value of variable $var is empty
;
}
else
{
echo The value of variable $var is not empty
;
}
echo "Variable direct test:
";
if ( $var ){
echo Variable $var exists!
;
}
else {
echo variable $var does not exist!
;
}
echo ------------- ---------------------
;
echo $var = null
;
echo isset test:
;
$var = null ;
if ( isset ( $var ))
{
echo variable $var exists!
;
}
echo "empty test:
";
if ( empty ( $var )){
echo The value of variable $var is empty
;
}
else
{
echo The value of variable $var is not empty
;
}
echo "Variable direct test:
";
if ( $var ){
echo variable $var exists!
;
}
else {
echo variable $var does not exist!
;
}
echo ----- -----------------------------
;
echo $var ="php "
;
echo isset test:
;
$var = "php";
if ( isset ( $var ))
{
echo variable $var exists!
;
}
echo "empty test:
";
if ( empty ( $var )){
echo variable The value of $var is empty
;
}
else
{
echo The value of variable $var is not empty
;
}
echo "variable Direct test:
";
if ( $var ){
echo variable $var exists!
;
}
else {
echo variable $var does not exist !
;
}
?>