Home  >  Article  >  Backend Development  >  In-depth understanding of the empty() function in PHP

In-depth understanding of the empty() function in PHP

PHPz
PHPzforward
2016-07-29 08:36:543232browse

This article mainly introduces the empty() function in PHP. It has certain reference value. Interested friends can refer to it. I hope it will be helpful to you!

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.

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 you to use the empty() function

to determine whether the string is empty. You can judge 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
* Exists and its value is "", 0, "0", NULL,, FALSE, array(), var $var; and objects without any attributes, then returns TURE
* 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

On the surface, it is easy It is misunderstood 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: TRUE if the variable does not exist If the variable exists and its value is "", 0, "0", NULL,, FALSE, array() , var $var; and objects without any attributes, then TRUE is returned. If the variable exists and the value is not "", 0, "0", NULL, , FALSE, array(), var $var; and objects without any attributes. , then return FALSE Version: PHP 3, PHP 4, PHP 5

<table width="760" border="1" align="center" cellpadding="3">
<?
$dir = &#39;./201006/24/&#39;;
$object[] = @readdir($dir);
  foreach ($object as $dirail)
if (empty($dirail)) {
?>
  <tr>
      <td align="center"><font color="red">目录下没有图片</font></td>
    </tr>
<? } else { ?>
    <tr>
      <td align="center"><font color="red">目录下有图片</font></td>
    </tr>
<? } ?>
</table>

For more related tutorials, please visit php programming from entry to master full set of video tutorials

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete