Home > Backend Development > PHP Problem > PHP determines that it is not empty

PHP determines that it is not empty

(*-*)浩
Release: 2023-02-27 11:46:01
Original
3050 people have browsed it

PHP determines that it is not empty

empty() function: detect whether the variable is "empty"

Description: Any uninitialized variable, the value is 0 or false Or empty string "" or null variable, empty array, object without any attributes, empty(variable) == true. (Recommended learning: PHP video tutorial)

Note 1: Uninitialized variables can also be detected as "empty" by empty.

Note 2: empty can only detect variables, not statements.

<?php
$a = 0;
$b = &#39;&#39;;
$c = array();
if (empty($a)) echo &#39;$a 为空&#39; . "<br/>";
if (empty($b)) echo &#39;$b 为空&#39; . "<br/>";
if (empty($c)) echo &#39;$c 为空&#39; . "<br/>";
if (empty($d)) echo &#39;$d 为空&#39; . "<br/>";
//结果
// $a 为空
// $b 为空
// $c 为空
// $d 为空
var_dump(empty(null));
var_dump(empty(0));
var_dump(empty(&#39;&#39;));
var_dump(empty([]));
//结果
// bool(true)
// bool(true)
// bool(true)
// bool(true)
Copy after login

var == null Function: Determine whether the variable is "empty"

Description: A variable whose value is 0 or false or an empty string "" or null, Empty arrays will be judged as null.

Note: The significant difference from empty is that var == null will report an error when the variable is not initialized.

is_null Function: Detect whether the variable is "null"

Description: When the variable is assigned a value of "null", the detection result is true.

Note 1: null is not case-sensitive: a=null;a=null;a = NULL makes no difference.

Note 2: Only when the value of the variable is "null", the detection result is true. 0, empty string, false, and empty array are all detected as false.

Note 3: When the variable is not initialized, the program will report an error.

The above is the detailed content of PHP determines that it is not empty. For more information, please follow other related articles on the PHP Chinese website!

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