Things to note when using empty() function in php_PHP Tutorial

WBOY
Release: 2016-07-13 16:56:52
Original
901 people have browsed it

When you use empty() to judge the number 0, empty() will also return true. That is to say, if your variable may have the number 0, it will be better to use isset() to judge! !

On the surface, it is easy to misunderstand that the empty() function is a function that determines whether a string is empty. In fact, it is not, and I suffered a lot because of it.

The empty() function is used to test whether the variable has been configured. If the variable already exists, is a non-empty string, or is non-zero, a false value is returned; otherwise, a true value is returned. Therefore, when the value of the string is 0, true is also returned, which is to execute the statement inside empty. This is the trap.

The code is as follows
 代码如下 复制代码

$a=array("1"=>"ddf");
var_dump(empty($a));///boolean false
$b=0;
var_dump(empty($b));  ///boolean true

Copy code

$a=array("1"=>"ddf");
var_dump(empty($a));///boolean false
$b=0;
var_dump(empty($b)); ///boolean true



I advise everyone to be careful about using the empty() function.

To determine whether a string is empty, you can determine it like this: if ($value=="") ...

* Format: bool empty (mixed var)
* Function: Check whether a variable is empty

* Return value:

* If the variable does not exist, return TRUE

* If the variable exists and its value is "", 0, "0", NULL,, FALSE, array(), var $var; and an object without any attributes, then TURE
 代码如下 复制代码

echo empty(strlen('be-evil.org'));

is returned * If the variable exists and the value is not "", 0, "0", NULL,, FALSE, array(), var $var; and an object without any attributes, then return FALSE

* Version: PHP 3, PHP 4, PHP 5

On the surface, it is easy to misunderstand that the empty() function is a function that determines whether a string is empty. In fact, it is not, and I suffered a lot because of it. The empty() function is used to test whether the variable has been configured. If the variable already exists, is a non-empty string, or is non-zero, a false value is returned; otherwise, a true value is returned. Therefore, when the value of the string is 0, true is also returned, which is to execute the statement inside empty. This is the trap. For example: Assume $value = 0; then empty($value)=false. I advise everyone to be careful about using the empty() function. To determine whether a string is empty, you can determine it like this: if ($value=="") ... Format: bool empty (mixed var) Function: Check whether a variable is empty Return value: If the variable does not exist, return TRUE If If the variable exists and its value is "", 0, "0", NULL,, FALSE, array(), var $var; and an object without any attributes, TRUE is returned. If the variable exists and its value is not "", 0, "0", NULL, FALSE, array(), var $var; and objects without any attributes, return FALSE Version: PHP 3, PHP 4, PHP 5

When you use empty to check the result returned by a function, an error will be reported: Fatal error: Can't use function return value in write context

For example:
代码如下 复制代码

$length = strlen('be-evil.org');

echo empty($length);
?>

The code is as follows Copy code
echo empty(strlen('be-evil.org'));<🎜> <🎜> <🎜> <🎜> <🎜>Go to the PHP manual and see the following text where the empty function is described: <🎜> <🎜>Note: empty() only checks variables as anything else will result in a parse error. In other words, the following will not work: empty(trim($name)).<🎜> <🎜>empty() only tests variables, testing anything that is not a variable will result in a parsing error!<🎜> <🎜> Therefore, we cannot use empty to directly detect the value returned by the function. The solution to the above example is as follows: <🎜>
The code is as follows<🎜> Copy code<🎜> <🎜>
<🎜> <🎜>$length = strlen('be-evil.org');<🎜> <🎜>echo empty($length);<🎜> ?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/631570.htmlTechArticleWhen using empty() to determine the number 0, empty() will also return true, that is, if your variable There may be a variable with the number 0. It would be better to use isset() to judge! ! On the surface...
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!