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

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

PHPz
Release: 2018-10-23 14:28:30
forward
3285 people have browsed it

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>
Copy after login

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

Related labels:
source:csdn.net
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