PHP empty and empty string differences

Source: Internet
Author: User
Tags true true

PHP empty and empty string differences if Var is a non-null or nonzero value, empty () returns FALSE. In other words, "", 0, "0", NULL, FALSE, Array (), Var $var; and objects that do not have any properties will be considered empty and return TRUE if Var is empty.

Empty () is the antonym of (Boolean) Var, except that it does not produce a warning when the variable has no value.

There are a lot of functions in PHP that are similar, but have subtle differences ...
Is_null, empty, isset, unset Let's take a look at the descriptions of these 4 functions first.
Isset to determine if a variable already exists (config)
unset Delete (release) the variable
Empty to determine if the variable is null
Is_null to determine if a variable is null
OK, it's starting to get people. So start, these 4 functions in addition to unset, the other 3 is the judgment function, unset first out, because he will not be mistaken, followed by is_null, we can see it as!isset, is a reverse operation of isset, the following table can be very clear about their relationship:
Variable Empty is_null isset
$a = "" True false True
$a =null true True false
var $a true true false
$a =array () True false true
$a =false true false true
$a =15 false False true
$a =1 false False true
$a =0 true false true
$a = "0" true false true
$a = "true" false false true
$a = "false" false false true
From this we can find that as long as the variable is "" or 0, or false and null, as long as these values are empty will return true, and isset is to determine whether the variable exists, as long as you this variable is not NULL or unassigned, the return result is true, and Is_ Null is exactly the inverse of the isset.


From this diagram, you can clearly see a connection:
Assume that $var is any type.
When empty ($var) is true, (bool) ($var) is false. Vice versa.
Same
When Is_null ($var) is true, Isset ($var) is false. Vice versa.
If:
$i = $j +1;
The Is_null ($j) here is true


Visible:
The isset is used to determine undefined conditions.
Empty can be judged undefined and defined, but empty, for 0 cases.
Is_null can be seen as a isset inverse function.
An If ($ expression) can be considered an empty inverse function
When the content to be judged is an array, the table lists only one case, and the following two easy-to-misunderstand situations
PHP code

$b []=null;
$b []=null;
Treated as if the array already exists and is not empty

$c []= "";
$c []= "";
Treated as if the array already exists and is not empty

Empty
(PHP 3, PHP 4, PHP 5)
Empty--Check if a variable is null
Describe
bool Empty (mixed Var)

If Var is a non-null or nonzero value, empty () returns FALSE. In other words, "", 0, "0", NULL, FALSE, Array (), Var $var; and objects that do not have any properties will be considered empty and return TRUE if Var is empty.
Empty () is the antonym of (Boolean) Var, except that it does not produce a warning when the variable has no value. See Convert to Boolean for more information.
A simple comparison of empty () with Isset ().

[Code]
<?php
$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 ';
}
?>
[/code]

Note: 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)).
Isset
(PHP 3, PHP 4, PHP 5)
Isset--Detect if the variable is set
Describe
BOOL Isset (mixed var [, mixed Var [, ...])

Returns TRUE if Var exists, otherwise FALSE is returned.
If a variable has been freed with unset (), it will no longer be isset (). If you use Isset () to test a variable that is set to NULL, FALSE is returned. Also note that a null byte ("s") 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 parsing errors. To detect if a constant is set, use the defined () function.
Is_null
(PHP 4 >= 4.0.4, PHP 5)
Is_null--detects if the variable is null
Describe
BOOL Is_null (mixed Var)

Returns TRUE if VAR is null, otherwise FALSE.
View the null type to know when the variable is considered null, and when it is not.

PHP empty and empty string differences

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.