Home > Backend Development > PHP Tutorial > How to determine if a variable is null in PHP? How to use the empty() function

How to determine if a variable is null in PHP? How to use the empty() function

青灯夜游
Release: 2023-04-04 16:52:01
Original
7527 people have browsed it

In PHP, you can use the built-in function empty() to check whether a variable is empty. The following article will introduce you to how to use the empty() function. I hope it will be helpful to you.

How to determine if a variable is null in PHP? How to use the empty() function

##empty() function

Basic syntax:

empty ( $var )
Copy after login

Parameters: The empty() function accepts a single parameter, as shown in the syntax above, as described below.

$ var: Variable used to check if it is empty.

Return value: Return false when $var exists and has a non-null non-zero value. Otherwise return true.

Note: Under PHP 5.5, the empty() function only supports variable checking, and anything else will cause a parsing error.

Simple example of empty() function

Let’s use an example to introduce how the empty() function uses to determine whether a variable is empty

<?php 
header("content-type:text/html;charset=utf-8");
echo "<br>";
$var1 = NULL; 
$var2 = ""; 
$var3 = "PHP中文网"; 
empty($var1) ? print_r("变量var1为空<br>") : print_r("变量var1不为空<br>"); 
  
empty($var2) ? print_r("变量var2为空<br>") : print_r("变量var2不为空<br>"); 
empty($var3) ? print_r("变量var3为空<br>") : print_r("变量var2不为空,值为:".$var3); 
?>
Copy after login

Output:

How to determine if a variable is null in PHP? How to use the empty() function

Explanation:

The following will be used by the empty() function Values ​​considered null:

1, "" (empty string)

2, 0 (0 as an integer)

3, 0.0 (0 as a floating point number )

4, "0" (0 as a string)

5, null

6, false

7, array() (an empty Array)

This article ends here. I hope it will be helpful to everyone’s learning. For more exciting content, you can pay attention to the relevant tutorial columns of the PHP Chinese website! ! !

The above is the detailed content of How to determine if a variable is null in PHP? How to use the 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