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
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 === OperatorUsing
2 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 substri
1 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
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
How to Create a Folder if It Doesn't Exist in PHP ? We can easily create a folder in PHP, but before that, you have to check if the folder or directory already exists or not. So In this article, you will learn both to Check and Create a folder or directory in PHP. Methods: file_exists(): It is an inbuilt function that is used to check whether a file
3 min read
How to check if a String contains a Specific Character in PHP ? In PHP, determining whether a string contains a specific character is a common task. Whether you're validating user input, parsing data, or performing text processing, PHP provides several methods to check for the presence of a particular character within a string efficiently. Here are some common a
2 min read
How to check a string starts/ends with a specific string in PHP ? In this article, we are going to learn how to check that a given string is starting or ending with a given specific string in PHP.We require the use of XAMPP, Netbeans to run this code.Input: "Geeks For Geeks"Starting with: "G"Output: "Geeks For Geeks Starts with String G"In PHP version 8+, there ar
2 min read
How to check an array is empty or not using jQuery ? In this article, we will check if an array is empty or not using jQuery. In JavaScript, arrays are a special type of object. If we use the typeof operator for arrays, it returns "object". We can use jQuery's isEmptyObject() method to check whether the array is empty or contains elements. The isEmpty
2 min read