Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • PHP Tutorial
  • PHP Exercises
  • PHP Array
  • PHP String
  • PHP Calendar
  • PHP Filesystem
  • PHP Math
  • PHP Programs
  • PHP Array Programs
  • PHP String Programs
  • PHP Interview Questions
  • PHP GMP
  • PHP IntlChar
  • PHP Image Processing
  • PHP DsSet
  • PHP DsMap
  • PHP Formatter
  • Web Technology
Open In App
Next Article:
How to check for empty string in PHP ?
Next article icon

How to Check Whether a Variable is Empty in PHP?

Last Updated : 16 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Given some values of variables, the task is to check whether the variable is empty or not in PHP. An empty variable can refer to a variable that has not been set, a variable that has been explicitly set to an empty value, or a variable that contains a value that is considered empty. In this article, we will explore different approaches to check whether a variable is empty in PHP.

Table of Content

  • Approach 1: Using empty() Function
  • Approach 2: Using isset() Function
  • Approach 3: Using strlen() Function and Custom Function
  • Approach 4: Using is_null() Function

Approach 1: Using empty() Function

The empty() function is a built-in PHP function that checks whether a variable is empty. A variable is considered empty if it does not exist, or if its value is equal to false, 0, "" (an empty string), "0" (a string containing zero), null, or an empty array.

PHP
<?php  $var1 = null; $var2 = ""; $var3 = 0; $var4 = "0"; $var5 = false; $var6 = []; $var7 = "Hello";  echo "Is variable 1 empty? " . (empty($var1) ? 'true' : 'false') . "\n"; echo "Is variable 2 empty? " . (empty($var2) ? 'true' : 'false') . "\n"; echo "Is variable 3 empty? " . (empty($var3) ? 'true' : 'false') . "\n"; echo "Is variable 4 empty? " . (empty($var4) ? 'true' : 'false') . "\n"; echo "Is variable 5 empty? " . (empty($var5) ? 'true' : 'false') . "\n"; echo "Is variable 6 empty? " . (empty($var6) ? 'true' : 'false') . "\n"; echo "Is variable 7 empty? " . (empty($var7) ? 'true' : 'false');  ?> 

Output
Is variable 1 empty? true Is variable 2 empty? true Is variable 3 empty? true Is variable 4 empty? true Is variable 5 empty? true Is variable 6 empty? true Is variable 7 empty? false

Explanation:

  • The empty() function returns true if the variable is empty and false otherwise.

Approach 2: Using isset() Function

The isset() function is another built-in PHP function that checks whether a variable is set and is not null.

PHP
<?php  $var1 = null; $var2 = ""; $var3 = 0; $var4 = "0"; $var5 = false; $var6 = []; $var7 = "Hello";  echo "Is variable 1 empty? " . (isset($var1) && $var1 === '' ? 'true' : 'false') . "\n"; echo "Is variable 2 empty? " . (isset($var2) && $var2 === '' ? 'true' : 'false') . "\n"; echo "Is variable 3 empty? " . (isset($var3) && $var3 === 0 ? 'true' : 'false') . "\n"; echo "Is variable 4 empty? " . (isset($var4) && $var4 === '0' ? 'true' : 'false') . "\n"; echo "Is variable 5 empty? " . (isset($var5) && $var5 === false ? 'true' : 'false') . "\n"; echo "Is variable 6 empty? " . (isset($var6) && $var6 === [] ? 'true' : 'false') . "\n"; echo "Is variable 7 empty? " . (isset($var7) && $var7 === '' ? 'true' : 'false');  ?> 

Output
Is variable 1 empty? false Is variable 2 empty? true Is variable 3 empty? true Is variable 4 empty? true Is variable 5 empty? true Is variable 6 empty? true Is variable 7 empty? false

Explanation:

  • The isset() function returns true if the variable is set and is not null. Also, Additional comparisons are used to check for specific empty values like an empty string, zero, or false.

Approach 3: Using strlen() Function and Custom Function

The strlen() function can be used to check if a string is empty by measuring its length. For other data types, a custom function can be created to check if the variable is empty.

Example: The strlen() function returns the length of a string. If the length is 0, the string is empty.

PHP
<?php  $var1 = null; $var2 = ""; $var3 = 0; $var4 = "0"; $var5 = false; $var6 = []; $var7 = "Hello";  echo "Is variable 1 empty? " . (empty($var1) ? 'true' : 'false') . "\n"; echo "Is variable 2 empty? " . (empty($var2) ? 'true' : 'false') . "\n"; echo "Is variable 3 empty? " . (empty($var3) ? 'true' : 'false') . "\n"; echo "Is variable 4 empty? " . (empty($var4) ? 'true' : 'false') . "\n"; echo "Is variable 5 empty? " . (empty($var5) ? 'true' : 'false') . "\n"; echo "Is variable 6 empty? " . (empty($var6) ? 'true' : 'false') . "\n"; echo "Is variable 7 empty? " . (empty($var7) ? 'true' : 'false');  ?> 

Output
Is variable 1 empty? true Is variable 2 empty? true Is variable 3 empty? true Is variable 4 empty? true Is variable 5 empty? true Is variable 6 empty? true Is variable 7 empty? false

Approach 4: Using is_null() Function

The is_null() function in PHP is used to check whether a variable is null. While it does not directly check for other empty values like empty strings or zero, it can be part of a broader strategy to determine if a variable is empty. This approach is especially useful when you need to specifically distinguish between null and other types of empty values.

Example: The following example demonstrates how to use the is_null() function to check if a variable is null, combined with additional checks for other empty values.

PHP
<?php function isVariableEmpty($var) {     if (is_null($var) || $var === "" || $var === false || $var === 0 || $var === "0" || (is_array($var) && empty($var))) {         return true;     }     return false; } $var1 = null; $var2 = "d"; $var3 = false; $var4 = 0; $var5 = "0"; $var6 = []; $var7 = "Non-empty string";  echo isVariableEmpty($var1) ? 'true' : 'false'; echo "\n"; echo isVariableEmpty($var2) ? 'true' : 'false';  ?> 

Output
true false



Next Article
How to check for empty string in PHP ?

B

blalverma92
Improve
Article Tags :
  • Web Technologies
  • PHP
  • PHP-Questions

Similar Reads

  • How to check for empty string in PHP ?
    In this article, we will see how to check for empty string in PHP. String is a set of characters. A string is said to be empty, if it contains no characters. We can use empty() function to check whether a string is empty or not. here we have some common approaches Table of Content Using empty() func
    4 min read
  • Check if a Set is Empty in JavaScript?
    These are the following ways to check whether the given set is empty or not: 1. Using size property - Mostly usedThe size property is used to get the size of the set. if it returns 0 then the set will be empty else it has some elements. [GFGTABS] JavaScript let s = new Set(); if (s.size === 0) conso
    2 min read
  • Bash Scripting - How to check If variable is Set
    In BASH, we have the ability to check if a variable is set or not. We can use a couple of arguments in the conditional statements to check whether the variable has a value.  In this article, we will see how to check if the variable has a set/empty value using certain options like -z, -v, and -n. Usi
    10 min read
  • How to check if a Variable Is Not Null in JavaScript ?
    In JavaScript, checking if a variable is not null ensures that the variable has been assigned a value and is not empty or uninitialized. This check is important for avoiding errors when accessing or manipulating data, ensuring that the variable holds valid, usable content before proceeding with oper
    4 min read
  • How to get a variable name as a string in PHP?
    Use variable name as a string to get the variable name. There are many ways to solve this problem some of them are discussed below: Table of Content Using $GLOBALSUsing $$ OperatorUsing debug_backtrace()Using get_defined_vars() and array_search()Method 1: Using $GLOBALS: It is used to reference all
    3 min read
  • How to Check a Column is Empty or Null in MySQL?
    In the databases, determining whether a column is empty or null is a common task. MySQL provides various techniques to perform this check, allowing users to filter and manipulate data efficiently. This article delves into the methods for checking if a column is empty or null in MySQL, outlining the
    4 min read
  • Check if a Map is Empty in JavaScript?
    These are the following ways to check whether the given map is empty or not: 1. Using size property - Mostly Usedyou can check if a Map is empty by using its size property. The size property returns the number of key-value pairs in the Map. If the size is 0, the Map is empty. [GFGTABS] JavaScript co
    2 min read
  • How to Check an Object is Empty using JavaScript?
    These are the following ways that can be used to Check an Object is Empty using JavaScript: 1. Using Object.keys() Method - Mostly usedThe Object.keys() method returns an array that contains the property names of an object. If the length of array is 0, then object is empty. [GFGTABS] JavaScript let
    2 min read
  • How to Check if Empty Array in C?
    Prerequisite: Array in C The array is a type of Data-structure that can store homogeneous data-type elements. The size of the array is fixed. Syntax: int arr[N]; here, the data type of the array is an integer, the name of an array is arr, and the size of the array is N. Example: C/C++ Code // C Prog
    3 min read
  • How to check whether the page is called from 'https' or 'http' in PHP ?
    The purpose of this article is to check whether the page is called from 'HTTPS' or 'HTTP', we can use the following two approaches. Approach 1: Check if the connection is using SSL and if the value of $_SERVER['HTTPS'] is set, then we can say that the connection is secured and called from 'HTTPS'. I
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences