How to check whether a String is empty in PHP
  John Mwaniki /   16 Dec 2021

How to check whether a String is empty in PHP

As a developer, you may get to a point where you are not sure if a string has a value or is empty. This can happen especially when the value of the string is dynamic and user-generated.

An empty string has a blank value in between the quotes (for quoted strings) or in between the opening and closing delimiter identifiers (for heredoc or nowdoc strings).

In this article, I will take you through the different methods of checking whether a string is empty or not.

Examples of empty strings:

<?php
//Example 1
$str1 = '';

//Example 2
$str2 = "";

//Example 3
$str3 = <<<ANYTEXT
ANYTEXT;

//Example 4
$str4 = <<<'EOT'
'EOT';

How to check if a string is empty in PHP

1. Using the empty() function

This is the simplest and the most straightforward way. The empty() is an inbuilt PHP function that checks whether a variable is empty or not.

Syntax

empty(variable)

The variable is a mandatory parameter that specifies which string variable to check.

The function returns FALSE if the variable exists and is not empty or TRUE if otherwise.

Example 1

<?php
$str = 'Hello World!';
if(empty($str)){
  echo 'The string is empty';
}
else{
  echo 'The string is not empty';
}

Output:

The string is not empty

Example 2

<?php
$str = <<<EOD
EOD;
if(empty($str)){
  echo 'The string is empty';
}
else{
  echo 'The string is not empty';
}

Output:

The string is empty

2. Using the strlen() function

The strlen() function returns the length of a string.

Syntax

strlen(string)

The string is a mandatory parameter that specifies the string in which to check the length.

The function returns the length of a string (in bytes) on success, and 0 if the string is empty. Therefore, to determine if a string is empty or not, we just check its length and compare it with 0. If it is greater than 0 then the string is not empty. Else, we consider it empty.

Example 1

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

Output:

The string is empty

Example 2

<?php
$str = "Hey there! May your code always work.";
if(strlen($str) > 0){
  echo "The string is not empty";
}
else{
  echo "The string is empty";
}

Output:

The string is not empty

3. Comparing it with an empty string

An empty string is equivalent to empty quotes, ie '' or "".

We just compare our string variable with empty quotes using the identical === operator. If the string is identical to the value of empty quotes, we conclude it's empty. If else, then we know it is not empty.

Example 1

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

Output:

The string is empty

Example 2

<?php
$str = "Do you find fun coding? Or you just have to?";
if($str === ""){
  echo "The string is empty";
}
else{
  echo "The string is not empty";
}

Output:

The string is not empty

That's all for this article.

That is how you check if a string is empty or not in PHP.