Home  >  Article  >  Backend Development  >  Detailed explanation of the difference between PHP isset() and empty()_PHP tutorial

Detailed explanation of the difference between PHP isset() and empty()_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:34:13813browse

PHP’s isset() function is generally used to detect whether a variable is set
Format: bool isset (mixed var [, mixed var [, ...]])

Function: Detection Whether the 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, then TRUE is returned
When checking multiple variables at the same time, 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 Note:
After using unset() to release a variable, it will no longer be 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 a value is empty

Format: bool empty (mixed var)

Function: Check whether a variable Is empty

Return value:

If the variable does not exist, return TRUE
If the variable exists and its value is "", 0, "0", NULL,, FALSE, array( ), var $var; and objects without any attributes, return TURE
If the variable exists and the value is not "", 0, "0", NULL,, FALSE, array(), var $var; and none Object of any attribute, returns FALSE
Version: PHP 3, PHP 4, PHP 5
More explanation:
The return value of empty() =! (boolean) var, but it will not be because the variable is not Define a warning message. 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 of empty() and isset()

Copy code The code is as follows:

< ?php
$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 is 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 called by variable functions.
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 Home. After reading this, it is basically the same:
Copy codeThe code is as follows:

error_reporting(E_ALL);
echo 'undefined$var
';
echo "isset test:< ;Br>";
if ( isset ( $var ))
{
echo 'Variable $var exists!
' ;
}
echo "empty test:< Br>";
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 = ''< /b>
';
echo "isset test:
";
$var = '';
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 "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:< ;Br>";
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 = null
';
echo 'isset test:
';
$var = null ;
if ( isset ( $var ))
{
echo ' Variable $var exists!
' ;
}
echo "empty test:
";
if ( empty ( $var )){
echo ' of variable $var The value 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 '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!
';
}
?>


When using PHP to write page programs, I often use variable processing functions to determine whether a variable value at the end of the PHP page is empty. At the beginning, I was used to I tried to use the empty() function, but found some problems, so I switched to the isset() function and the problem disappeared.
As the name suggests, empty() determines whether a variable is "empty", and isset() determines whether a variable has been set. It is this so-called "as the name implies" that made me take some detours at the beginning: when a variable value is equal to 0, empty() will also be true (True), so some accidents will occur. It turns out that although empty() and isset() are both variable processing functions, they are both used to determine whether the variable has been configured, but they have certain differences: empty will also detect whether the variable is empty or zero. When a variable value is 0, empty() considers the variable to be equivalent to being empty, which is equivalent to not being set.
For example, to detect the $id variable, when $id=0, use empty() and isset() to detect whether the variable $id has been configured. Both will return different values ​​- empty() considers it not configured, isset () can get the value of $id:

Copy code The code is as follows:

$id=0;
empty($id)?print "It's empty .":print "It's $id .";
//Result: It's empty .
print "
";
!isset( $id)?print "It's empty .":print "It's $id .";
//Result: It's 0 .


This means that when we use the variable processing function, when the variable may have a value of 0, be careful when using empty(). It is more sensible to replace it with isset at this time.
When the URL tail parameter of a php page appears id=0 (for example: test.php?id=0), try to compare:

Copy code The code is as follows:

if(empty($id)) $id=1; - If id=0, id will also be 1
if(!isset($id) )) $id=1; - If id=0, id will not be 1


You can run the following code separately to detect the above inference:

Copy code The code is as follows:

if(empty($id)) $id=1;
print $id; // get 1
if(!isset($id)) $id=1;
print $id; //get 0


To talk about their connection, their common point is empty( ) and isset() are both variable processing functions. Their function is to determine whether the variable has been configured. It is precisely because of their great similarity in the process of processing variables that they have insufficient understanding of their relationship. If you only consider the two functions empty() and isset() themselves, it will make people more confused. Look at it from another angle. The processing objects of empty() and isset() are nothing more than undefined variables, 0, and empty strings.
If the variable is 0, empty() will return TRUE, isset() will return TRUE;

If the variable is an empty string, empty() will return TRUE, isset() will return TRUE ;
If the variable is not defined, empty() will return TRUE and isset() will return FLASE;

The explanation of empty() in the manual is as follows:

Description bool empty( mixed var)
If var is a non-empty or non-zero value, empty() returns FALSE. In other words, "", 0, "0", NULL, FALSE, array(), var $var; and objects without any properties will be considered empty, and TRUE will be returned if var is empty.
The explanation of isset() in the manual is as follows:

isset() detects whether the variable is set

Description bool isset ( mixed var [, mixed var [, ...]] )

Returns TRUE if var exists, otherwise returns FALSE.

If a variable has been released using unset(), it will no longer be isset(). If you use isset() to test a variable that is set to NULL, it will return FALSE. Also note that a NULL byte ("?") is not equivalent to PHP's NULL constant.
Warning: isset() can only be used with variables, as passing any other arguments will cause a parsing error. If you want to check whether a constant has been set, you can use the defined() function.

When you want to determine whether a variable has been declared, you can use the isset function
When you want to determine whether a variable has been assigned data and is not empty, you can use the empty function
When you want to determine whether a variable has been assigned data and is not empty, you can use the empty function If the variable exists and is not empty, first use the isset function and then use the empty function

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/322452.htmlTechArticlePHP’s isset() function is generally used to detect whether a variable is formatted: bool isset ( mixed var [, mixed var [, ...]] ) Function: Check whether the variable is set. Return value: If the variable does not exist, return...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn