The difference between empty () and isset () in PHP

Source: Internet
Author: User

For beginners of PHP, the difference between empty () and isset () usage is hard to figure out, and it's hard to figure out the difference in their usage without careful thought.

Let's talk about what they have in common:

    1. Can determine whether a variable is empty;

    2. Returns a Boolean type, which is either true or false.

Here's a specific distinction between their usage:

    1. Isset () is used to detect if a variable is set and can only be used for variables, because passing any other parameter will result in parsing errors. To detect if a constant is set, use the defined () function. If a variable has been freed with unset (), it will no longer be isset (). If you use Isset () to test a variable that is set to NULL, FALSE is returned. (Note that a null byte ("s") is not equivalent to a null constant in PHP)

    2. Empty () is used to check if a variable is empty.

    3. The biggest difference between them is the judgment of 0, if it is judged empty by empty, with isset it is not empty, for example:

      <?php

var $a = 0;

Empty ($a) returns true

if (empty ($a)) {

echo "Judgment result is empty"

}

Isset ($a) returns true

if (Isset ($a)) {

echo "Judgment result is not empty"

}

?>

-----------------------------------------------------------------------------

Isset to see if a variable has been set and not empty (determine if a variable is set and was not null)

Empty to see if a variable is empty "", 0, "0", NULL, FALSE, Array (), $var ($var is a variable with no assignment)

It is also important to note that from PHP5.5, empty () can only support variables, and anything else passed in will result in a parsing error.

In other words, the expression like the one below will no longer work with empty (trim ($name)). We should use trim ($name) = = False to make judgments instead of it.

The result of the above code execution

The difference between empty () and isset () in PHP

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.