How to check for empty string in PHP ?
Last Updated :
07 Aug, 2024
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
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";
}
?>
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";
}
?>
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;
}
?>
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";
}
?>
OutputThe 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;
}
?>
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.";
}
?>
OutputThe 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.";
}
?>
OutputThe 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.";
}
?>
OutputThe string is not empty.
Similar Reads
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
JavaScript Tutorial JavaScript is a programming language used to create dynamic content for websites. It is a lightweight, cross-platform, and single-threaded programming language. It's an interpreted language that executes code line by line, providing more flexibility.JavaScript on Client Side: On the client side, Jav
11 min read
Web Development Web development is the process of creating, building, and maintaining websites and web applications. It involves everything from web design to programming and database management. Web development is generally divided into three core areas: Frontend Development, Backend Development, and Full Stack De
5 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
React Interview Questions and Answers React is an efficient, flexible, and open-source JavaScript library that allows developers to create simple, fast, and scalable web applications. Jordan Walke, a software engineer who was working for Facebook, created React. Developers with a JavaScript background can easily develop web applications
15+ min read
React Tutorial React is a powerful JavaScript library for building fast, scalable front-end applications. Created by Facebook, it's known for its component-based structure, single-page applications (SPAs), and virtual DOM,enabling efficient UI updates and a seamless user experience.Note: The latest stable version
7 min read
JavaScript Interview Questions and Answers JavaScript is the most used programming language for developing websites, web servers, mobile applications, and many other platforms. In Both Front-end and Back-end Interviews, JavaScript was asked, and its difficulty depends upon the on your profile and company. Here, we compiled 70+ JS Interview q
15+ min read
Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
3-Phase Inverter An inverter is a fundamental electrical device designed primarily for the conversion of direct current into alternating current . This versatile device , also known as a variable frequency drive , plays a vital role in a wide range of applications , including variable frequency drives and high power
13 min read
Backpropagation in Neural Network Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and
9 min read