The difference between empty and isset

Source: Internet
Author: User
We believe that as a novice, each time we use empty () and isset (), there will be such doubts: what is the difference between the two? When do I need to use empty ()? When to use Isset ()? Today I'll talk to you about the difference between the two and the application scenario.

1. Summary of differences:

Difference One: Empty () is a function, it has all the properties of the function, there is a return value, you can use the function return value as a parameter, you can use dynamic variable access, etc.;

Isset () is a statement that is an intrinsic logical structure of PHP, such as foreach,for,continue, that cannot be accessed using dynamic variables, and cannot use function return values as arguments;

$a = ' empty '; $a (' err ');//trueempty (Intval (' 1FA '));//false;

Difference two: When a $ A is defined, regardless of its value is 0,false,null, "equal null value, empty () return value is True;isset () The return value is also ture,isset only when validating an undefined variable will return false;

For example:

$a = 0;//' 0 ', false,null, ' ', ' 0.0 ', etc. empty values empty ($a);//trueempty (' AAA ');//falseempty ($b);//trueisset ($a);//trueisset ($b) ;//false, $b undefined

2. Application Scenario:

A. When judging whether a variable is defined, Isset () is available;

Application: The form is submitted with a value of isset ($_post[' submit ') to determine whether to submit the form.

B. When judging whether a variable is empty (you do not need to know whether it is assigned), you can use empty ();

Application: When validating a form, verify that the required value is empty.

C. When judging that a variable already exists and is not empty, you can use Isset () first, then empty ();

Application: When uploading files, it is necessary to determine that $_files has been assigned and is not empty.

The above describes the difference between empty and isset, including aspects of the content, I hope that the PHP tutorial interested in a friend helpful.

  • 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.