Thursday, October 23, 2025
HomeLanguagesHow to check for empty string in PHP ?

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.

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

Example 2:

PHP




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


Output

Not empty
RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS