Can empty () replace isset () When php checks whether a variable is set? _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
Can empty () replace isset () When php checks whether a variable is set ?. Let's first review empty and issetempty to check whether a variable is empty, 0, 0, NULL, FALSE, array (), var $ var; and objects without any attributes will be considered empty. for example, let's first review empty and isset.

Empty-check whether a variable is empty

"", 0, "0", NULL, FALSE, array (), var $ var; and objects without any attribute will be considered empty. If var is empty, returns TRUE.

Isset-check whether the variable is set

Check whether the variable is set and not NULL. That is, if the variable is not set, false is returned. if the variable is NULL, false is returned.

The php manual explains clearly. if a variable is not set, what results will empty return?

[Php]

Var_dump (empty ($ undefined ));

Var_dump (empty ($ undefined); the result is: bool (true)

It can be seen that empty can also be used to check whether the variables are set. So should we use empty instead of isset?

I think isset should be used only to judge whether a variable is stored or not, because the meaning of isset is clear, that is, it is used to facilitate reading the code.

You can use

[Php]

If (! Empty ($ var) replaces if (isset ($ var )&&! Empty ($ var ))

If (! Empty ($ var) replaces if (isset ($ var )&&! Empty ($ var) when you want to determine whether a variable is NULL, you must not use isset.

Here we will introduce some content:

Performance of isset and empty?

In fact, the performance of isset and empty is very good, with little difference. One more point is that they are all statements, not functions. if you want to take a look at the specific content, you can refer to the article "differences between isset and is_null" by laruence.

The statement cannot be called by a variable function. In addition, in isset and empty, the return value of the function cannot be used as a parameter. for example, this cannot be done.

[Php]

Empty (intval ('3 '));

// Fatal error: Can't use function return value in write context

Empty (intval ('3 '));

// Fatal error: Can't use function return value in write context

However, the latest PHP5.5 release supports this feature, that is, it can use any expression in empty:

[Php]

Function always_false (){

Return false;

}

If (empty (always_false ())){

Echo "This will be printed. \ n ";

}

Function always_false (){

Return false;

}

If (empty (always_false ())){

Echo "This will be printed. \ n ";

}

Output:

This will be printed.

Receivempty checks whether a variable is NULL, 0, 0, NULL, FALSE, array (), var $ var, and objects without any attributes are considered empty, such...

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.