Home  >  Article  >  Backend Development  >  What does empty mean in php

What does empty mean in php

青灯夜游
青灯夜游Original
2022-03-03 13:49:554008browse

In PHP, empty means "empty". It is a built-in function used to check whether a variable is empty. The syntax is "empty($var)"; when a variable value is 0, the empty character String, "0.0", ""0"", NULL, FALSE, empty array, empty() considers this variable to be equal to empty.

What does empty mean in php

The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

empty means "empty".

What does empty mean in php

empty() is a built-in function in PHP that determines whether a variable is "empty".

empty will also detect whether the variable is empty or zero. When a variable value is 0, empty() considers the variable to be equivalent to being empty, which is equivalent to not being set.

Example:

$id=0;
empty($id)?print "It's empty .":print "It's $id ."; 
//结果:It's empty .
print "<br>";
!isset($id)?print "It&#39;s empty .":print "It&#39;s $id .";
//结果:It&#39;s 0 .

When a variable does not exist, or its value is equal to FALSE, then it is considered not to exist. empty() does not generate a warning if the variable does not exist.

Note: Prior to PHP 5.5, empty() only supported variables; anything else would cause a parsing error. In other words, the following code will not work:

empty(trim($name))

Instead, you should use:

trim($name) == false

empty() and will not generate a warning, even if the variable does not exist. This means that empty() is essentially equivalent to !isset($var) || $var == false.

Return value:

Returns FALSE when var exists and is a non-empty and non-zero value, otherwise returns TRUE.

The following variables will be considered empty:

  • "" (empty string)

  • 0 (0 as an integer)

  • 0.0 (0 as a floating point number)

  • "0" (0 as a string)

  • NULL

  • FALSE

  • array() (an empty array)

  • $ var; (a variable declared but without a value)

Example:

<?php
$ivar1=0;
$istr1=&#39;Runoob&#39;;
if (empty($ivar1))
{
    echo &#39;$ivar1&#39; . " 为空或为 0。" . PHP_EOL;
}
else
{
    echo &#39;$ivar1&#39; . " 不为空或不为 0。" . PHP_EOL;
}
if (empty($istr1))
{
    echo &#39;$istr1&#39; . " 为空或为 0。" . PHP_EOL;
}
else
{
    echo &#39;$istr1&#39; . " 字符串不为空或不为0。" . PHP_EOL;
}
?>

Output:

$ivar1 为空或为 0。
$istr1 字符串不为空或不为0。

Recommended Study: "PHP Video Tutorial"

The above is the detailed content of What does empty mean in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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