PHP best way to check whether a string is empty or not

PHP best way to check whether a string is empty or not

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

The Real Cost of Checking String Emptiness in PHP

When writing PHP, determining whether a string holds data is a frequent task. A common pattern emerging from legacy codebases or quick scripts involves checking if a variable is set and not equal to an empty string:

if (!isset($str) || $str !== '') {
    // do something
}

This approach handles the nuances of loose typing well by explicitly verifying existence before value. However, many developers prefer a more concise alternative using strlen:

if (strlen($str) > 0) {
    // something
}

While this second method is cleaner to read, it introduces a subtle but critical vulnerability regarding PHP's type system. The primary risk lies in how PHP handles string casting when passing non-string variables to strlen.

The Casting Trap

The function strlen() expects a string or an integer as its argument. If you pass a variable that is not already a string, PHP attempts to cast it. Specifically, if the variable is an object, PHP casts the object to the string "Object" before calculating the length.

class User {
    public $name = 'Alice';
}

$obj = new User();

// This evaluates to true because strlen("Object") is 6
if (strlen($obj) > 0) {
    echo "Length: " . strlen($obj); 
}

In this scenario, the object is valid and has properties, but strlen returns a non-zero value based on its internal string representation. This leads to logical errors where code intended for empty strings incorrectly treats objects or other non-string types as having content.

The first pattern using isset() combined with !== '' avoids this issue entirely because it never invokes type casting logic within the condition itself; it strictly checks for array key existence and strict inequality against an empty string.

Handling Null Values

There is another specific edge case regarding null. In PHP, passing null to strlen() results in a warning (in non-strict environments) or returns 0 in newer versions depending on configuration, but the behavior has shifted over time. More importantly, relying solely on length ignores the distinction between a string that was never assigned versus one explicitly set to null.

Consider a scenario where you are validating user input from a database field that might contain NULL rather than an empty string:

// If $data contains NULL
$data = null; 

if (strlen($data) > 0) { 
    // This block will not execute in modern PHP versions without warnings,
    // but the logic assumes a string context which is violated.
}

Using isset() implicitly handles null values by returning false if the variable is set to null, whereas strlen() might trigger a warning or behave unexpectedly depending on the exact runtime version and error reporting settings.

Modern Best Practices

To balance readability with safety, the most robust pattern often involves checking for emptiness directly without relying on length calculations that invite casting issues. The idiomatic PHP approach leverages the truthiness of the variable while accounting for empty strings:

if (!empty($str)) {
    // do something
}

The empty() function is specifically designed to return true if the value is an empty array, empty string, null, false, 0, or "0". This covers the vast majority of validation needs without triggering type casts. However, it treats the integer 0 as empty, which might not be desirable if you are strictly distinguishing between a numeric zero and an empty state in a specific context.

If strict distinction is required—specifically needing to treat null, an unset variable, or an empty string as invalid, while treating everything else (including 0) as valid—you must use the strict comparison operator:

if ($str !== '') {
    // do something
}

This check assumes the variable exists. If there is a risk of the variable being undefined, you must wrap it in an existence check or use the null coalescing operator logic. The combination of isset() and strict inequality provides the highest level of safety against type casting surprises while remaining readable.

Ultimately, strlen() should be reserved for performance-critical paths where you genuinely need byte-length calculations on confirmed strings, not for boolean emptiness checks.

Our network