How to determine whether a variable is empty in php

(*-*)浩
Release: 2023-02-26 11:48:01
Original
2108 people have browsed it

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 为空' . "";
Copy after login

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
Copy after login

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!

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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!