Differences between isset () and empty () functions in PHP _ PHP Tutorial

Source: Internet
Author: User
Differences between isset () and empty () functions in PHP. The isset () function of PHP is generally used to check whether the variable is set in the format boolisset (mixedvar [, mixedvar [,...]). function: checks whether the returned value is set. if the variable does not exist, the isset () function of PHP is used to check whether the variable is set.
Format: bool isset (mixed var [, mixed var [,...])
Function: checks whether variables are set.
Return value:
If the variable does not exist, FALSE is returned.
If the variable exists and its value is NULL, FALSE is returned.
If the variable exists and the value is not NULL, true is returned.
When multiple variables are checked at the same time, TRUE is returned only when each individual item meets the previous requirement; otherwise, the result is FALSE.
Version: PHP 3, PHP 4, PHP 5
More instructions:
When unset () is used to release a variable, it is no longer an isset ().
The PHP function isset () can only be used for variables. passing any other parameter will cause a parsing error.
Checks whether the defined () function has been set for constants.

PHP's empty () function determines whether the value is null.
Format: bool empty (mixed var)
Function: checks whether a variable is empty.
Return value:
Returns TRUE if the variable does not exist.
If a variable exists and its values are "", 0, "0", NULL, FALSE, array (), var $ var, and an object without any attribute, true is returned.
If a variable exists and its values are not "", 0, "0", NULL, FALSE, array (), var $ var, or an object without any attribute, FALSE is returned.
Version: PHP 3, PHP 4, PHP 5
More instructions:
Empty () return value =! (Boolean) var, but no warning is generated because the variable is not defined. For more information, see convert to a Boolean value.
Empty () can only be used for variables. passing any other parameter will cause Paser error and terminate the operation.
Checks whether the defined () function has been set for constants.
Example: a simple comparison between empty () and isset ()
The code is as follows:
$ 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: Because this is a language structure rather than a function, it cannot be called by variable functions.
Note: empty () only detects variables, and any non-variables can cause parsing errors. In other words, the following statement does not work: empty (addslashes ($ name )).
The following is the code of a detailed example of isset and empty functions tested by the script house. after reading the code, it is basically the same:
The code is as follows:

Error_reporting (E_ALL );
Echo Undefined $ var
;
Echo "isset test:
";
If (isset ($ var ))
{
The echo variable $ var exists!
;
}
Echo "empty test:
";
If (empty ($ var )){
The value of echo variable $ var is empty.
;
}
Else
{
The value of echo variable $ var is not empty.
;
}
Echo "direct test of variables:
";
If ($ var ){
The echo variable $ var exists!
;
}
Else {
The echo variable $ var does not exist!
;
}
Echo ----------------------------------
;
Echo $ Var = \
;
Echo "isset test:
";
$ Var =;
If (isset ($ var ))
{
The echo variable $ var exists!
;
}
Echo "empty test:
";
If (empty ($ var )){
The value of echo variable $ var is empty.
;
}
Else
{
The value of echo variable $ var is not empty.
;
}
Echo "direct test of variables:
";
If ($ var ){
The echo variable $ var exists!
;
}
Else {
The echo variable $ var does not exist!
;
}
Echo ----------------------------------
;
Echo $ Var = 0
;
Echo isset test:
;
$ Var = 0;
If (isset ($ var ))
{
The echo variable $ var exists!
;
}
Echo "empty test:
";
If (empty ($ var )){
The value of echo variable $ var is empty.
;
}
Else
{
The value of echo variable $ var is not empty.
;
}
Echo "direct test of variables:
";
If ($ var ){
The echo variable $ var exists!
;
}
Else {
The echo variable $ var does not exist!
;
}
Echo ----------------------------------
;
Echo $ Var = null
;
Echo isset test:
;
$ Var = null;
If (isset ($ var ))
{
The echo variable $ var exists!
;
}
Echo "empty test:
";
If (empty ($ var )){
The value of echo variable $ var is empty.
;
}
Else
{
The value of echo variable $ var is not empty.
;
}
Echo "direct test of variables:
";
If ($ var ){
The echo variable $ var exists!
;
}
Else {
The echo variable $ var does not exist!
;
}
Echo ----------------------------------
;

Echo $ Var = "php"
;
Echo isset test:
;
$ Var = "php ";
If (isset ($ var ))
{
The echo variable $ var exists!
;
}

Echo "empty test:
";
If (empty ($ var )){
The value of echo variable $ var is empty.
;
}
Else
{
The value of echo variable $ var is not empty.
;
}
Echo "direct test of variables:
";
If ($ var ){
The echo variable $ var exists!
;
}
Else {
The echo variable $ var does not exist!
;
}
?>

The evaluate () function is generally used to check whether the variable is set in the format: bool isset (mixed var [, mixed var [,...]). function: checks whether the returned value is set for a variable. if the variable does not exist, the returned value is...

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.