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$args
If 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 2empty
Can process undefined parameters to avoid warnings. In additionempty
It 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$args
Already defined. The first type can be used. If not, the second method is recommended.
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.