Can empty () replace isset () when php checks whether a variable is set?

Source: Internet
Author: User

Let's first review empty and isset empty-check whether a variable is empty "", 0, "0", NULL, FALSE, array (), var $ var; objects without any attributes will be considered empty. If var is empty, TRUE isset is returned to check whether the variable is set and whether the detection 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. php manual has clearly explained the result. If the variable is not set, what is the result returned by empty? [Php] var_dump (empty ($ undefined); the result is: bool (true) can detect whether the empty variable is 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. Next, let's look at the performance of isset and empty? In fact, the performance of isset and empty is very good, with little difference. One more thing is that they are all statements, not functions. I want to take a look at the specific content in this regard, you can see that the statement isset and is_null in this article cannot be called by variable functions. In addition, isset and empty cannot accept the return values of functions as parameters, for example, [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, currently, 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 ";} will output: This will be printed.

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.