How to detect if a variable is empty in php
In program development, we encounter a lot of judgments. Sometimes when we need to judge whether a variable is empty, isset(), empty(), and is_null() are always Confused, let’s introduce their differences below
1. isset, determines whether the variable has been initialized
isset(), determines whether the variable has been set, it will not determine whether the variable is empty. And can be used to determine whether the elements in the array have been defined
Code description:
<?php $a = ''; $a['c'] = ''; if (!isset($a)) echo '$a 未被初始化' . ""; if (!isset($b)) echo '$b 未被初始化' . ""; if (isset($a['c'])) echo '$a 已经被初始化' . ""; // 显示结果为 // $b 未被初始化 // $a 已经被初始化 ?>
2. empty, detect whether the variable is "empty"
empty(), any An uninitialized variable, a variable with a value of 0 or false or an empty string "" or null, an empty array, or an object without any attributes will be judged as empty()==true
Note 1: Uninitialized variables can also be detected as "empty" by empty
Note 2: empty can only detect variables, not statements
Code description:
<?php $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 为空' . ""; ?>
3. $a == null, 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 obvious difference from empty is that if the variable $a is not initialized, $a == null will report an error.
Code description:
<?php $a = 0; $b = array(); if ($a == null) echo '$a 为空' . ""; if ($b == null) echo '$b 为空' . ""; if ($c == null) echo '$b 为空' . ""; // 显示结果为 // $a 为空 // $b 为空 // Undefined variable: c ?>
4. is_null, detects whether the variable is "null"
is_null(), 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 makes no difference
Note 2: The detection result is only when the value of the variable is "null" is true, 0, empty string, false, and empty array are all detected as false
Note 3: When the variable is not initialized, an error will also be reported
Code description:
<?php $a = null; $b = false; if (is_null($a)) echo '$a 为NULL' . ""; if (is_null($b)) echo '$b 为NULL' . ""; if (is_null($c)) echo '$c 为NULL' . ""; // 显示结果为 // $a 为NULL // Undefined variable: c ?>
5. $a === null, detect whether the variable is "null", and the type of the variable must also be "null"
Description: When a variable is assigned a value of "null", the type of the variable must also be "null" When it is also "null", the detection result is true
Note 1: When it is judged to be "null", it has the same effect as is_null
Note 2: When the variable is not initialized, the program also An error will be reported
Summary:
In PHP, "NULL" and "empty" are two concepts.
isset is mainly used to determine whether a variable has been initialized.
empty can set variables with values of "false", "empty", "0", "NULL", and "uninitialized" Judge as TRUE
is_null Only judge the variable whose value is "NULL" as TRUE
$a == null Judge as "false", "empty", "0", "NULL" " variables are all judged to be TRUE
$a === null Only variables with a value of "NULL" are judged to be TRUE
, so when we judge whether a variable is really "NULL" , mostly use is_null to avoid interference from values such as "false" and "0".
The above is the detailed content of How to detect if a variable is empty in php. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Environment variables are the path to the location (or environment) where applications and programs run. They can be created, edited, managed or deleted by the user and come in handy when managing the behavior of certain processes. Here's how to create a configuration file to manage multiple variables simultaneously without having to edit them individually on Windows. How to use profiles in environment variables Windows 11 and 10 On Windows, there are two sets of environment variables – user variables (apply to the current user) and system variables (apply globally). However, using a tool like PowerToys, you can create a separate configuration file to add new and existing variables and manage them all at once. Here’s how: Step 1: Install PowerToysPowerTo

Strict mode was introduced in PHP7, which can help developers reduce potential errors. This article will explain what strict mode is and how to use strict mode in PHP7 to reduce errors. At the same time, the application of strict mode will be demonstrated through code examples. 1. What is strict mode? Strict mode is a feature in PHP7 that can help developers write more standardized code and reduce some common errors. In strict mode, there will be strict restrictions and detection on variable declaration, type checking, function calling, etc. Pass
![Internal error: Unable to create temporary directory [Resolved]](https://img.php.cn/upload/article/000/000/164/168171504798267.png?x-oss-process=image/resize,m_fill,h_207,w_330)
Windows system allows users to install various types of applications on your system using executable/setup files. Recently, many Windows users have started complaining that they are receiving an error named INTERNALERROR:cannotCreateTemporaryDirectory on their systems while trying to install any application using an executable file. The problem is not limited to this but also prevents the users from launching any existing applications, which are also installed on the Windows system. Some possible reasons are listed below. Run the executable to install without granting administrator privileges. An invalid or different path was provided for the TMP variable. damaged system

Instance variables in Java refer to variables defined in the class, not in the method or constructor. Instance variables are also called member variables. Each instance of a class has its own copy of the instance variable. Instance variables are initialized during object creation, and their state is saved and maintained throughout the object's lifetime. Instance variable definitions are usually placed at the top of the class and can be declared with any access modifier, which can be public, private, protected, or the default access modifier. It depends on what we want this to be

Python is widely used in a wide range of fields with its simple and easy-to-read syntax. It is crucial to master the basic structure of Python syntax, both to improve programming efficiency and to gain a deep understanding of how the code works. To this end, this article provides a comprehensive mind map detailing various aspects of Python syntax. Variables and Data Types Variables are containers used to store data in Python. The mind map shows common Python data types, including integers, floating point numbers, strings, Boolean values, and lists. Each data type has its own characteristics and operation methods. Operators Operators are used to perform various operations on data types. The mind map covers the different operator types in Python, such as arithmetic operators, ratio

Using Ajax to obtain variables from PHP methods is a common scenario in web development. Through Ajax, the page can be dynamically obtained without refreshing the data. In this article, we will introduce how to use Ajax to get variables from PHP methods, and provide specific code examples. First, we need to write a PHP file to handle the Ajax request and return the required variables. Here is sample code for a simple PHP file getData.php:

PHP function introduction—strpos(): Check whether a variable is a string In PHP, is_string() is a very useful function, which is used to check whether a variable is a string. When we need to determine whether a variable is a string, the is_string() function can help us achieve this goal easily. Below we will learn about how to use the is_string() function and provide some related code examples. The syntax of the is_string() function is very simple. it only needs to

jQuery is a JavaScript library widely used in web development. It provides many simple and convenient methods to operate web page elements and handle events. In actual development, we often encounter situations where we need to determine whether a variable is empty. This article will introduce several common methods of using jQuery to determine whether a variable is empty, and attach specific code examples. Method 1: Use the if statement to determine varstr="";if(str){co
