Home  >  Article  >  Backend Development  >  How to determine whether a variable is empty in php

How to determine whether a variable is empty in php

(*-*)浩
(*-*)浩Original
2019-10-12 09:46:082124browse

How to determine whether a variable is empty in php

empty function: detect whether the variable is "empty"

Description: Any uninitialized variable, the value is 0 or false or empty string "" or null variables, empty arrays, and objects without any attributes will be judged as empty==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, but not statements

$a = 0;
$b = '';
$c = array();
if (empty($a)) echo '$a 为空' . "";
if (empty($b)) echo '$b 为空' . "";
if (empty($c)) echo '$c 为空' . "";
if (empty($d)) echo '$d 为空' . "";

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

Note: Variables and empty arrays with a value of 0 or false or an empty string "" or null 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.

$a = 0;
$b = array();
if ($a == null) echo '$a 为空' . "";
if ($b == null) echo '$b 为空' . "";
if ($c == null) echo '$b 为空' . "";
// 显示结果为
// $a 为空
// $b 为空
// Undefined variable: c

The above is the detailed content of How to determine whether a variable is empty in php. 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