PHP: 5 Ways to Check if a String is Empty

Updated: January 9, 2024 By: Guest Contributor Post a comment

In PHP, accurately checking if a string is empty is crucial for input validation, form processing, and data handling. This guide illustrates multiple ways to do this, each with its strengths and situations where it is more appropriate.

Solution 1: Using empty()

Description: The empty() function checks if a variable is an empty string, zero, false, or unset.

  1. Use the variable with empty().
  2. Analyse the boolean result: true if the string is empty.

Example:

$string = '';
if (empty($string)) {
    echo 'The string is empty.';
} else {
    echo 'The string is not empty.';
}

Notes: empty() also considers ‘0’ as empty, which may be undesirable.

Solution 2: Comparison with Empty String Literal

Description: Direct comparison with an empty string literal avoids false positives with empty() when checking strings.

  1. Compare the string directly to ”.
  2. Analyse the boolean result.

Example:

$string = '';
if ($string === '') {
    echo 'The string is empty.';
} else {
    echo 'The string is not empty.';
}

Notes: Using the identity operator (===) prevents issues with type juggling.

Solution 3: Using strlen()

Description: strlen() function returns the length of the string. If it’s 0, the string is empty.

  1. Apply strlen() to the string.
  2. Check if the result is 0.

Example:

$string = '';
if (strlen($string) === 0) {
    echo 'The string is empty.';
} else {
    echo 'The string is not empty.';
}

Notes: strlen() is binary-safe and multibyte character safe, making it reliable for all types of strings.

Solution 4: Using ctype_space()

Description: ctype_space() checks if all characters in a string are whitespace. This does not treat an empty string as whitespace.

  1. Check the string with ctype_space() or against an empty string.
  2. Consider that an empty string will result in false.

Example:

$string = '';
if (ctype_space($string) || $string === '') {
    echo 'The string is empty or only whitespace.';
} else {
    echo 'The string is not empty.';
}

Notes: ctype_space() won’t consider an empty string as whitespace but identifies strings consisting only of whitespace.

Solution 5: Using trim() and checking length

Description: Trimming the string removes whitespace from the beginning and end of the string. Then you can check its length.

  1. Apply trim() to the string.
  2. Use strlen() or empty() to check if the trimmed string is empty.

Example:

$string = "  \t\n";
if (empty(trim($string))) {
    echo 'The string is empty.';
} else {
    echo 'The string is not empty.';
}

Notes: trim() only removes whitespace from the start and end of the string, not in the middle.

Conclusion

Knowing various methods to check if a string is empty in PHP enables developers to understand and select the most appropriate technique for their context. From a simple empty() call to more sophisticated ctype_space() checks, understanding the behavior, performance implications, and pitfalls of each method ensures robust, error-free applications.