PHP isset () and empty () the use of different detailed _php basis

Source: Internet
Author: User
Tags mixed parse error
PHP's Isset () function is commonly used to detect whether a variable is set
Format: bool Isset (mixed var [, mixed Var [, ...]])

Function: Detect whether the variable is set

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
Version: PHP 3, PHP 4, PHP 5
More Description:
After you use unset () to free a variable, it is no longer a isset ().
php function Isset () can only be used for variables, and passing any other parameter will result in parsing errors.
Detects whether a constant is set to use the defined () function.

php's Empty () function determines whether the value is null

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 an object without any attributes, return ture
If the variable exists and the value is not "", 0, "0", NULL, FALSE, Array (), Var $var; and an object without any attributes, return FALSE
Version: PHP 3, PHP 4, PHP 5
More Description:
return value of empty () =! (Boolean) Var, but does not produce a warning message because the variable is undefined. See Convert to Boolean to get more information.
Empty () can only be used for variables, and passing any other parameter will cause Paser error to terminate the operation.
Detects whether a constant is set to use the defined () function.
Example: A simple comparison of empty () and isset ()
Copy Code code as follows:

<?php
$var = 0;
The result is true because the $var is empty
if (empty ($var)) {
Echo ' $var is either 0 or not set at all ';
}
The result is false because the $var has been set
if (!isset ($var)) {
Echo ' $var is isn't set at all ';
}
?>

Note: Because this is a language structure and not a function, it cannot be invoked by a variable function.
Note: empty () Only detects variables, and detecting anything that is not variable will result in parsing errors. In other words, the following statement will not work: Empty (Addslashes ($name)).
The following is a detailed example of a isset and empty function that has been tested by a cloud-dwelling community:
Copy Code code as follows:

<?php
Error_reporting (E_all);
Echo ' <B> undefined $var</b><br> ';
echo "Isset test:<br>";
if (Isset ($var))
{
Echo ' variable $var exists!<br> ';
}
echo "Empty test:<br>";
if (empty ($var)) {
The value of ECHO ' variable $var is null <Br> ';
}
Else
{
The value of ECHO ' variable $var is not null <Br> ';
}
echo "Variable direct test:<br>";
if ($var) {
Echo ' variable $var exists!<br> ';
}
else {
echo ' variable $var does not exist!<br> ';
}
Echo '----------------------------------<br> ';
echo ' <B> $var = \ ' </b><Br> ';
echo "Isset test:<br>";
$var = ';
if (Isset ($var))
{
Echo ' variable $var exists!<br> ';
}
echo "Empty test:<br>";
if (empty ($var)) {
The value of ECHO ' variable $var is null <Br> ';
}
Else
{
The value of ECHO ' variable $var is not null <Br> ';
}
echo "Variable direct test:<br>";
if ($var) {
Echo ' variable $var exists!<br> ';
}
else {
echo ' variable $var does not exist!<br> ';
}
Echo '----------------------------------<br> ';
echo ' <B> $var = 0</b><br> ';
Echo ' isset test:<br> ';
$var = 0;
if (Isset ($var))
{
Echo ' variable $var exists!<br> ';
}
echo "Empty test:<br>";
if (empty ($var)) {
The value of ECHO ' variable $var is null <Br> ';
}
Else
{
The value of ECHO ' variable $var is not null <Br> ';
}
echo "Variable direct test:<br>";
if ($var) {
Echo ' variable $var exists!<br> ';
}
else {
echo ' variable $var does not exist!<br> ';
}
Echo '----------------------------------<br> ';
echo ' <B> $var = Null</b><br> ';
Echo ' isset test:<br> ';
$var = null;
if (Isset ($var))
{
Echo ' variable $var exists!<br> ';
}
echo "Empty test:<br>";
if (empty ($var)) {
The value of ECHO ' variable $var is null <Br> ';
}
Else
{
The value of ECHO ' variable $var is not null <Br> ';
}
echo "Variable direct test:<br>";
if ($var) {
Echo ' variable $var exists!<br> ';
}
else {
echo ' variable $var does not exist!<br> ';
}
Echo '----------------------------------<br> ';

echo ' <B> $var = ' php ' </b><Br> ';
Echo ' isset test:<br> ';
$var = "PHP";
if (Isset ($var))
{
Echo ' variable $var exists!<br> ';
}

echo "Empty test:<br>";
if (empty ($var)) {
The value of ECHO ' variable $var is null <Br> ';
}
Else
{
The value of ECHO ' variable $var is not null <Br> ';
}
echo "Variable direct test:<br>";
if ($var) {
Echo ' variable $var exists!<br> ';
}
else {
echo ' variable $var does not exist!<br> ';
}
?>


When using PHP to write a page program, I often use the variable processing function to determine whether the PHP page tail parameter of a variable value is empty, at the beginning I used to use the empty () function, but found some problems, so instead of the isset () function, the problem is no longer.
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 I began with some detours: when a variable value is equal to 0 o'clock, empty () will also be set (True), so there will be some surprises. Originally, Empty () and isset (), although they are variable processing functions, are used to determine whether the variables have been configured, they are a certain difference: empty will also detect whether the variable is empty, zero. When a variable value is 0,empty (), the variable equals null, which is equivalent to no setting.
For example, to detect $id variables, when $id = 0 o'clock, using empty () and isset () to detect whether the variable $id has been configured, both will return a different value--empty () do not think that the configuration, isset () can get $id value:

Copy Code code as follows:

$id = 0;
Empty ($id) print "It's empty.":p rint "it's $id."
Result: It ' s empty.
print "<br>";
!isset ($id) print "It's empty.":p rint "it's $id."
Result: It ' s 0.


This means that when we use a variable-handling function, when the variable is likely to have a value of 0, use empty () to be careful, and it's wiser to replace it with Isset.
When the URL tail parameter of a PHP page appears id=0 (for example: test.php?id=0), try to compare:

Copy Code code as follows:

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


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

Copy Code code as follows:

if (empty ($id)) $id = 1;
Print $id; Get 1
if (!isset ($id)) $id = 1;
Print $id; Get 0


Their connection, in common, is that empty () and isset () are variable-processing functions that determine whether variables are configured, because they have a great similarity in the process of processing variables, leading to inadequate understanding of their relationship. The empty () and isset () of the two functions themselves will make the person more confused and put it in a different angle. The processing objects of empty () and isset () are no more than undefined variables, 0, empty strings.
If the variable is 0, then empty () returns True,isset () to return true;

If the variable is an empty string, then empty () returns True,isset () to return true;
If the variable is undefined, empty () returns True,isset () and returns flase;

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

description bool Empty (mixed Var)
Empty () returns FALSE if Var is a non-null or Non-zero value. In other words, "", 0, "0″, NULL, FALSE, Array (), Var $var; and objects that do not have any attributes will be considered empty and TRUE if Var is empty.
The interpretation of Isset () in the manual is as follows:

Isset () detects whether a variable is set

description bool Isset (mixed var [, mixed Var [, ...]])

Returns TRUE if VAR exists, otherwise returns 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 notice is a NULL byte ("?"). is not equivalent to a NULL constant in PHP.
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.

You can use the Isset function when you want to determine whether a variable has been declared
You can use the empty function when you want to determine whether a variable has been given data and is not empty
When you want to judge a variable exists and NOT NULL first isset function and then use the empty function

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.