When checking whether a variable is set in PHP, can empty() replace isset()? _PHP Tutorial

WBOY
Release: 2016-07-15 13:21:24
Original
1356 people have browsed it

Let’s first review empty and isset

empty — Check if a variable is empty
"", 0, "0", NULL, FALSE, array(), var $var; and objects without any attributes will be considered empty. If var is empty, TRUE
isset — Check whether a variable is set
Check whether the variable is set and not NULL. That is: if the variable is not set, return false; if the variable is NULL, return false
The PHP manual explains it clearly, but if a variable is not set, what result does empty return?
[php]
var_dump(empty($undefined));
var_dump(empty($undefined));The result is: bool(true)
It can be seen that empty can also serve the purpose of detecting whether a variable is set. So should we use empty instead of isset?
I think if it is just to determine whether a variable exists, then isset should be used, because the meaning of isset is very clear, it is used for this, and it is also conducive to reading the code.
When you want to determine the existence of a variable and also determine that the variable is not 0, '', empty array, etc., you can use
[php]
if(!empty($var)) instead of if(isset($var) && !empty($var))
if(!empty($var)) instead of if(isset($var) && !empty($var)) When you want to determine whether a variable is NULL, you must not use isset.
Let’s introduce some content below:
Performance of isset and empty?
In fact, the performance of isset and empty is very good, there is not much difference. Another point to mention is that they are all statements, not functions. If you want to take a closer look at this aspect, you can read this article by Brother Niao "The Difference between isset and is_null"
The
statement cannot be called by a variable function. In addition, the return value of the function cannot be accepted as a parameter in isset and empty. For example, this is not possible
[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, the ability to use arbitrary expressions 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.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/477203.htmlTechArticleLet’s first review empty and isset empty to check whether a variable is empty, 0, 0, NULL, FALSE, array(), var $var; and objects without any properties will be considered empty, such as...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template