PHP empty () Check whether a variable is empty _php tips

Source: Internet
Author: User
empty-Check if a variable is empty

Statement a bug 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 empty () is the antonym of (Boolean) Var, except that the argument does not produce a warning when the variable does not have a value. See Convert to Boolean to get more information.

Example #1 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 it is a language constructor and not a function, it cannot be called by a variable function.

Note:

Empty () detects only variables, and detecting anything that is not a variable will result in parsing errors. In other words, the following statement will not work: Empty (Addslashes ($name)).

The following things are considered to be empty:

"" (An empty string)
0 (0 as an integer)
0.0 (0 as a float)
"0" (0 as a string)
Null
FALSE
Array () (an empty array)
var $var; (a variable declared, but without a value in a class)
Understanding of "Empty array": Array () (an empty array)

Copy Code code as follows:

<?php
$array 1=array ();
Print_r ($array 1);
if (Empty ($array 1)) {
echo is an empty array (an empty array) for empty ();
}
else{
Echo ' is a non-empty array (an noempty array) for empty ();
}
?>
//Display results: ######################
Array
(
)
//empty array
for empty () ###############################
<?php
$array 1=array ();
$array 1[]= ';
Print_r ($array 1);
if (Empty ($array 1)) {
echo is an empty array (an empty array) for empty ();
}
else{
Echo ' is a non-empty array (an noempty array) for empty ();
}
?>
//Show Results: ######################
Array
(
[0] =>
)
//For Empty () is a non-empty array ( An noempty array)
//###############################
//This is not an empty array, because it has an element of NULL character (""), note and null character ("" (An empty string) difference;

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.