PHP isset () function differs from empty () function _php Tutorial

Source: Internet
Author: User
Empty to determine whether a variable is "empty", isset to determine whether a variable has been set. It is this so-called "as the name implies", so I began to take some detours: when a variable value equals 0 o'clock, empty () will also be set (True)

Empty to determine whether a variable is "empty", isset to determine whether a variable has been set. It is this so-called "as the name implies", so I began to take some detours: when a variable value equals 0 o'clock, empty () will also be set (true), so there will be some accidents. Originally, empty and isset are variable processing functions, they 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 the value of a variable is 0,empty that the variable equals null, it is equivalent to no setting.
For example, the detection of $id variables, when $id=0, with empty and isset to detect whether the variable $id has been configured, both will return different values--empty think that there is no configuration, isset can obtain the value of $id:

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

This means that when we use the variable handler function, when the variable may appear with a value of 0, use empty to be careful, it is wiser to replace it with Isset.

When the URL trailing parameter of a PHP tutorial page appears id=0 (for example: test.php?id=0), try comparing:

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

The following code can be run separately to detect the above inference:

if (empty ($id)) $id=1;
Print $id; Get 1

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


See an example


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


Because this is a language structure and not a function, it cannot be called by a variable function.
Note: empty () detects only variables and detects anything that is non-variable will result in parsing errors. In other words, the statement behind will not work: Empty (Addslashes ($name)).
Here is the code for a detailed example of the isset and empty functions that have been tested by the script house, which is basically the same.

Error_reporting (E_all);
Echo '$var Not defined
';
echo "Isset test:
";
if (Isset ($var))
{
Echo ' Variable $var exists!
' ;
}
echo "Empty test:
";
if (empty ($var)) {
The value of ECHO ' variable $var is null
';
}
Else
{
echo ' variable $var value is not empty
';
}
echo "Variable direct test:
";
if ($var) {
Echo ' Variable $var exists!
';
}
else {
echo ' 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)) {
The value of ECHO ' variable $var is null
';
}
Else
{
echo ' variable $var value is not empty
';
}
echo "Variable direct 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)) {
The value of ECHO ' variable $var is null
';
}
Else
{
echo ' variable $var value 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)) {
The value of ECHO ' variable $var is null
';
}
Else
{
echo ' variable $var value 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)) {
The value of ECHO ' variable $var is null
';
}
Else
{
echo ' variable $var value is not empty
';
}
echo "Variable direct test:
";
if ($var) {
Echo ' Variable $var exists!
';
}
else {
echo ' variable $var does not exist!
';
}
?>


http://www.bkjia.com/PHPjc/445423.html www.bkjia.com true http://www.bkjia.com/PHPjc/445423.html techarticle Empty to determine whether a variable is empty, Isset determines whether a variable has been set. It is this so-called name, so that I began to go a few detours: when a variable value equals 0 o'clock, ...

  • Related Article

    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.