Home > Backend Development > PHP Problem > What is the usage of php empty()

What is the usage of php empty()

藏色散人
Release: 2023-03-09 19:10:01
Original
2371 people have browsed it

php empty() function is used to check whether a variable is empty. The syntax of this function is "bool empty (mixed $var)", where the parameter "$var" represents the variable to be checked.

What is the usage of php empty()

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

empty() function is used to check whether a variable is null.

empty() Determines whether a variable is considered empty. 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.

empty() After version 5.5, it supports expressions, not just variables.

Version requirements: PHP 4, PHP 5, PHP 7

Syntax

bool empty ( mixed $var )
Copy after login

Parameter description:

$var: Variable to be checked.

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

Instead, you should use:

trim($name) == false
Copy after login

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:

"" (空字符串)
0 (作为整数的0)
0.0 (作为浮点数的0)
"0" (作为字符串的0)
NULL
FALSE
array() (一个空数组)
$var; (一个声明了,但是没有值的变量)
Copy after login

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;
}
?>
Copy after login

The execution result is as follows:

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

Recommended learning:《PHP video tutorial

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

Related labels:
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