In PHP, if ($ args) and if (! Empty ($ args)

Source: Internet
Author: User
Tags type null
There is often confusion during encoding. to judge whether a variable is not empty, use the method 1: if ($ args) {... dosomething...} Method 2: if (! Empty ($ args) {... dosomething...} has the same effect on execution efficiency? Thank you for your answers! There is often confusion during encoding to determine whether a variable is not empty:
Method 1: if ($ args) {... do something ..}
Method 2: if (! Empty ($ args) {... do something ...}

Are these two methods of execution more efficient?
Thank you for your answers!

Reply content:

There is often confusion during encoding to determine whether a variable is not empty:
Method 1: if ($ args) {... do something ..}
Method 2: if (! Empty ($ args) {... do something ...}

Are these two methods of execution more efficient?
Thank you for your answers!

Not the same. if $ args = 0, then $ args = false

In most cases! Empty is more secure. it can take unset variables as parameters and does not report errors. for example:

if($args[2] == 1) {}

If element 2 does not exist in the array, an error is returned. It should be written like this

if(!empty($args[2]) && $args[2] == 1) {}

The first method is$argsIf it is undefined, a warning will appear. any errors in PHP will cause a great deal of performance loss, which is mainly caused by the Error handling mechanism in PHP.

Method 2emptyCan process undefined parameters to avoid warnings. In additionemptyIt is a command rather than a function, and the running efficiency is not much slower than the first one.

In short, if you can guarantee$argsAlready defined. The first type can be used. If not, the second method is recommended.

  • Method 1: Involved in type conversion

  • Method 2: empty determines non-null values, including

Int 0 float 0.0 string '0' string' array () boolean falseNULL $ var (a variable defined but null)

The key lies in the judgment with if () and empty.
Copy the logs, but only if:

Boolean value FALSE. integer value 0. float type 0.0 null string ''and string '0'; // Here is an array that focuses on no elements. objects that do not contain any members. (php 4.0 only) Special type NULL other types of data are set to TRUE by default (including resource type)-1 is also TRUE (regardless of positive or negative)

This is the result of the if statement. for empty, see the document.

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.