Php empty () check whether a variable is empty

Source: Internet
Author: User

Empty-check whether a variable is empty

Report a bug description

Bool empty (mixed $ var)
If var is a non-null or non-zero value, empty () returns FALSE. In other words, "", 0, "0", NULL, FALSE, array (), var $ var; and objects without any attributes will be considered empty, if var is null, TRUE is returned.

Empty () is the antonym of (boolean) var except when the variable is not set. For more information, see convert to a Boolean value.

Example #1 a simple comparison between empty () and isset.
Copy codeThe Code is as follows:
<? Php
$ Var = 0;
// The result is true because $ var is null.
If (empty ($ var )){
Echo '$ var is either 0 or not set at all ';
}
// The result is false because $ var has been set.
If (! Isset ($ var )){
Echo '$ var is not set at all ';
}
?>

Note: A language constructor rather than a function cannot be called by a variable function.

Note:

Empty () only detects variables, and any non-variables will cause parsing errors. In other words, the following statement does 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 codeThe Code is as follows:
<? Php
$ Array1 = array ();
Print_r ($ array1 );
If (empty ($ array1 )){
Echo 'for empty () is an empty array (an empty array )';
}
Else {
Echo 'non-empty array (an noempty array) 'For empty )';
}
?>
// Display the result :######################
Array
(
)
// For empty (), it is an empty array
###############################
<? Php
$ Array1 = array ();
$ Array1 [] = '';
Print_r ($ array1 );
If (empty ($ array1 )){
Echo 'for empty () is an empty array (an empty array )';
}
Else {
Echo 'non-empty array (an noempty array) 'For empty )';
}
?>
// Display the result :######################
Array
(
[0] =>
)
// A non-empty array (an noempty array) for empty)
//###############################
// This is not an empty array, because it has an element that is a null character (""). Be sure to distinguish it from a null character ("" (an empty string;

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.