Differences between empty and isset

Source: Internet
Author: User
: This article mainly introduces the differences between empty and isset. For more information about PHP tutorials, see. I believe that as a newbie, every time we use empty () and isset (), we will have the following questions: What is the difference between them? When do I need to use empty ()? When to use isset ()? Today, I want to tell you the differences between the two and their application scenarios.

1. Summary of differences:

Difference 1: empty () is a function that has all the attributes of a function, has a return value, can use the function return value as a parameter, can use dynamic variables to access;

Isset () is a statement. it is an inherent logical structure of php, such as foreach, for, and continue. it cannot be accessed using dynamic variables or use function return values as parameters; example:

$a = 'empty';$a('err');//TRUEempty(intval('1fa'));//FALSE;

Difference 2: After $ a is defined, whether its value is 0, false, null, ''or other null values, empty () returns true; isset () the return value is true. isset returns false only when verifying undefined variables;

For example:

$ A = 0; // '0', false, null, '', '0. 0' and other null values empty ($ a); // trueempty ('aaa'); // falseempty ($ B); // trueisset ($ ); // trueisset ($ B); // false, $ B undefined

2. application scenarios:

A. When determining whether a variable is defined, isset () is available ();

Application: when submitting a form, use the value of isset ($ _ POST ['submit ']) to determine whether to submit the form.

B. When determining whether a variable is null (you do not need to know whether it is assigned a value), empty () is available ();

Application: During form verification, you must verify whether the required value is null.

C. when determining whether a variable already exists and is not empty, use isset () and empty ();

Application: when uploading a file, you must determine that $ _ FILES has been assigned a value and is not empty.

The above introduces the differences between empty and isset, including the content, hope to be helpful to friends who are interested in PHP tutorials.

Related Article

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.