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 Whether a Variable is Empty in PHP?
Next article icon

How to check for empty string in PHP ?

Last Updated : 07 Aug, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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() function
  • Using the strlen() function
  • Using trim() and ==
  • Using === operator
  • Using the mb_strlen() Function
  • Using isset() Function
  • Using strlen() and trim() Function

Using empty() function

The function is used to check whether the string is empty or not. It will return true if the string is empty.

Syntax:

bool empty(string)

Parameter: Variable to check whether it is empty or not.

Return Value: If string is empty, it returns true and false otherwise.

Example 1: PHP program to check whether the string is empty or not.

PHP
<?php  // Consider a string which is empty $s = "";  // Return a message if string is empty if(empty($s)) {     echo "Empty string"; } else {     echo "Not empty"; }  ?> 

Output
Empty string

Using the strlen() function

Using the strlen() function, you can check if a string is empty by evaluating its length. If `strlen($string) === 0`, the string is empty. This approach ensures precise length measurement, distinguishing between empty strings and other empty values like `NULL` or `0`.

Example:

PHP
<?php // Consider a string which is empty $s = "";  // Return a message if string is empty if (strlen($s) === 0) {     echo "Empty string"; } else {     echo "Not empty"; } ?> 

Output
Empty string

Using trim() and ==

The trim() and == approach removes whitespace from both ends of a string using trim(). Then, it compares the result with an empty string using ==, which checks if the trimmed string is empty.

Example

PHP
<?php     $str ="  ";         if (trim($str) =="") {     echo "String is empty" . PHP_EOL;      }else{     echo "String is not empty" . PHP_EOL;  }  ?> 

Output
String is empty 

Using === operator

The === operator in PHP checks both the value and the type of the variable. This approach directly compares the string with an empty string, ensuring that only strings that are truly empty (with no characters) are considered empty.

Example: This method is straightforward and ensures that only truly empty strings (with no characters) are detected.

PHP
<?php $string = "";  if ($string === "") {     echo "The string is empty"; } else {     echo "The string is not empty"; } ?> 

Output
The string is empty

Using the mb_strlen() Function

Using the mb_strlen() function in PHP, you can check for an empty string, including multibyte characters. It returns the string length, and if the result is 0, the string is empty. This is useful for handling non-ASCII text accurately.

Example

PHP
<?php     $str = "  ";     if (mb_strlen(trim($str)) === 0) {         echo "String is empty" . PHP_EOL;     } else {         echo "String is not empty" . PHP_EOL;     } ?> 

Output
String is empty 

Using isset() Function

The isset() function checks if a variable is set and is not NULL. Combining isset() with a check for an empty string ensures that the variable exists and is not empty.

Example:

PHP
<?php  $string = ""; if (isset($string) && $string === "") {     echo "The string is empty."; } else {     echo "The string is not empty."; }  ?> 

Output
The string is empty.

Using strlen() and trim() Function

Another approach to check if a string is empty in PHP involves combining the strlen() and trim() functions. This method ensures that even strings that only contain whitespace are considered empty.

Example: This example demonstrates how to use the strlen() and trim() functions to check if a string is empty.

PHP
$string = "    ";  if (strlen(trim($string)) === 0) {     echo "The string is empty."; } else {     echo "The string is not empty."; } ?> 

Output
The string is empty. 

Using strlen() and empty() Function

This approach combines the strlen() function to check the length of the string and the empty() function to ensure that the variable is not only empty but also set. This method provides a thorough check for empty strings.

Example:

PHP
<?php $string = "   "; // Return a message if the string is empty after trimming and using empty() if (empty($string) && strlen(trim($string)) === 0) {     echo "The string is empty."; } else {     echo "The string is not empty."; } ?> 

Output
The string is not empty. 

Next Article
How to Check Whether a Variable is Empty in PHP?

M

manojkumarreddymallidi
Improve
Article Tags :
  • Web Technologies
  • PHP
  • PHP-string
  • PHP-function
  • PHP-Questions

Similar Reads

  • How to check if File Exists in PHP ?
    To check whether any file is existing or not then we can use the below-mentioned PHP function. To find the existence of the files, we use file_exists() function. This function is used to check whether a file or directory exists or not. Syntax: file_exists( $path ) Parameters: This function accept on
    1 min read
  • How to Check Whether a Variable is Empty in PHP?
    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,
    5 min read
  • How to Check empty/undefined/null String in JavaScript?
    Empty strings contain no characters, while null strings have no value assigned. Checking for an empty, undefined, or null string in JavaScript involves verifying if the string is falsy or has a length of zero. Here are different approaches to check a string is empty or not. 1. Using === OperatorUsin
    2 min read
  • How to convert string to boolean in PHP?
    Given a string and the task is to convert given string to its boolean. Use filter_var() function to convert string to boolean value. Examples: Input : $boolStrVar1 = filter_var('true', FILTER_VALIDATE_BOOLEAN); Output : true Input : $boolStrVar5 = filter_var('false', FILTER_VALIDATE_BOOLEAN); Output
    2 min read
  • How to remove the first character of string in PHP?
    Remove the very first character of a given string in PHP Examples: Input : GeeksforgeeksOutput : eeksforgeeksInput :, Hello geek!Output : Hello geek!Explanation: In PHP to remove characters from the beginning we can use ltrim but in that, we have to define what we want to remove from a string i.e. r
    3 min read
  • How to check if a String Contains a Substring in PHP ?
    Checking whether a string contains a specific substring is a common task in PHP. Whether you're parsing user input, filtering content, or building search functionality, substring checking plays a crucial role. MethodsBelow are the following methods by which we can check if a string contains a substr
    1 min read
  • How to get the last character of a string in PHP ?
    In this article, we will find the last character of a string in PHP. The last character can be found using the following methods. Using array() Method: In this method, we will find the length of the string, then print the value of (length-1). For example, if the string is "Akshit" Its length is 6, i
    2 min read
  • How to read each character of a string in PHP ?
    A string is a sequence of characters. It may contain integers or even special symbols. Every character in a string is stored at a unique position represented by a unique index value. Here are some approaches to read each character of a string in PHP Table of Content Using str_split() method - The st
    4 min read
  • PHP to check substring in a string
    In this article, we will see how to check the substring in a string. We are given two strings & we have to check whether the second string is a substring of the first string or not using PHP built-in strpos() function. This function is case-sensitive, which means that it treats upper-case and lo
    2 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
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