Detailed explanation of php empty() function to check whether the variable is empty

怪我咯
Release: 2023-03-13 21:36:01
Original
1705 people have browsed it

empty() only detects variables, detecting anything that is not a variable will result in a parsing error. In other words, the following statements will have no effect: empty(addslashes($name))

empty — Checks whether a variable is empty

Report a bug Description

bool empty (mixed $var)
If var is a non-empty or non-zero value, empty() returns FALSE. In other words, "", 0, "0", NULL, FALSE, array(), var $var; and objects without any attributes will be considered empty , returns TRUE if var is empty.

In addition to not generating a warning when the variable is not set, empty() is the antonym of (boolean) var. See Converting to Boolean for more information.

Example #1 A simple comparison between empty() and isset().

The code is as follows:

<?php 
$var = 0; 
// 结果为 true,因为 $var 为空 
if (empty($var)) { 
echo &#39;$var is either 0 or not set at all&#39;; 
} 
// 结果为 false,因为 $var 已设置 
if (!isset($var)) { 
echo &#39;$var is not set at all&#39;; 
} 
?>
Copy after login

Note: Because it is a language constructor rather than a function, it cannot be called by the variable function .

Note:

empty() only tests variables, testing anything that is not a variable will result in a parsing error. In other words, the following statement will not work: empty(addslashes($name)).

The above is the detailed content of Detailed explanation of php empty() function to check whether the variable is 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