Home > Backend Development > PHP Problem > How to use php empty function

How to use php empty function

青灯夜游
Release: 2023-02-22 19:18:02
Original
6529 people have browsed it

The empty() function is a built-in function in PHP used to check whether a variable is empty. The syntax is empty (var), which returns FALSE when var exists and has a non-empty non-zero value, otherwise it returns TRUE.

How to use php empty function

How to use php empty() function?

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

Syntax:

empty (var)
Copy after login

Parameters: This function accepts a single parameter, as shown in the above syntax

● var: Variable to check if it is empty.

Return value: Returns FALSE when var exists and has a non-empty, non-zero value. Otherwise returns TRUE.

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.

Note: PHP 5.5 version supports expressions, not just variables. Under PHP 5.5, empty() only supports variables, anything else will cause a parsing error. The following statement will not work (trim(var)). Instead, use trim(name) == false.

Version requirements: PHP 4, PHP 5, PHP 7

These values ​​are considered null values:

● "" (empty string )

● 0 (0 is an integer)

● 0.0 (0 is a floating point number)

● “0” (0 is a string)

● NULL

● FALSE

● array() (an empty array)

Let’s take an example to see how to use the php empty() function.

<?php 
$var1 = 0; 
$var2 = 0.0; 
$var3 = "0"; 
$var4 = NULL; 
$var5 = false; 
$var6 = array(); 
$var7 = ""; 

empty($var1) ? print_r("True\n") : print_r("False\n"); 
empty($var2) ? print_r("True\n") : print_r("False\n"); 
empty($var3) ? print_r("True\n") : print_r("False\n"); 
empty($var4) ? print_r("True\n") : print_r("False\n"); 
empty($var5) ? print_r("True\n") : print_r("False\n"); 
empty($var6) ? print_r("True\n") : print_r("False\n"); 
empty($var7) ? print_r("True\n") : print_r("False\n"); 
empty($var8) ? print_r("True\n") : print_r("False\n"); 
  
?>
Copy after login

Output:

True
True
True
True
True
True
True
True
Copy after login

The above is the detailed content of How to use php empty function. 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