Empty and isset usage differences in php

Source: Internet
Author: User
In php program development, many friends are confused about how isset and empty are used or when isset and empty functions are used. I will introduce the differences between the two. isset-check whether PHPisset () is set for the variable ()... in php program development, many friends are confused about how isset and empty are used or when isset and empty functions are used. I will introduce the differences between the two.

Isset-check whether the variable is set

PHP isset () is used to detect whether one or more variables are set. if the detected variables exist, TRUE is returned; otherwise, FALSE is returned.

The sample code is as follows:

 

The elements in the array are also valid. the code is as follows:

$ A = array ('test' => 1, 'Hello' => NULL );

Var_dump (isset ($ a ['test'); // TRUE

Var_dump (isset ($ a ['Foo'); // FALSE

Var_dump (isset ($ a ['Hello'); // FALSE

// 'Hello' is equal to NULL, so it is considered to be unassigned.

// If you want to check the NULL key value, try the following method.

Var_dump (array_key_exists ('hello', $ a); // TRUE

Note:

• Isset () can only be used to detect Variables. passing any other parameter will cause parsing errors.

• Isset () is a language structure rather than a function, so it cannot be called by variable functions.

Empty-check whether a variable is empty

If the variable is not null or a non-zero value, empty () returns false.

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. the code is as follows:

 

The instance comparison code is as follows:

 ";} Else {echo" variable does not exist!
";} If (emptyempty ($ is_var) {echo" variable is empty!
";} Else {echo" variable is not empty!
";}?>


Permanent address:

Reprint at will ~ Please bring the tutorial URL ^

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.