Home  >  Article  >  Backend Development  >  How to use php empty function

How to use php empty function

青灯夜游
青灯夜游Original
2019-05-26 11:12:246491browse

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)

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"); 
  
?>

Output:

True
True
True
True
True
True
True
True

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!

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