php is null when empty?

php is null when empty?

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

Stefan Bogdanescu

Founder & Senior Architect

Why Empty Strings Compare True to NULL in PHP

The behavior you are observing is not a bug; it is a deliberate feature rooted in PHP's loose typing system and its use of weak equality operators. When you execute the code snippet where $a is an empty string and compare it using ==, PHP prints "is null". This happens because of a specific rule known as type juggling, which automatically converts operands to a common type before performing the comparison.

In your example:

$a = '';
if ($a == NULL) {
    echo 'is null'; // Triggers because '' is converted to 0 (integer), and NULL also becomes 0
}

Here, the empty string '' has a boolean value of false. In PHP's integer context, this translates to the number 0. The constant NULL represents a lack of value. When the operator == is used, PHP attempts to make both sides match so they can be compared numerically. Since an empty string evaluates to 0 and NULL also evaluates to 0, the comparison returns true.

This mechanism is powerful for quick checks but dangerous if relied upon in critical logic. Relying on == allows unexpected type conversions that can introduce subtle bugs. For instance, a non-empty string containing only whitespace might behave differently depending on how it is cast, or a boolean false variable might be treated as an empty string. This ambiguity is why modern PHP development strongly favors strict equality operators.

The Strict Equality Operator

To resolve this confusion and write more predictable code, you should use the triple equals sign (===). This operator checks both value and type. Unlike ==, it does not attempt to convert types.

$a = '';
if ($a === NULL) {
    echo 'is null'; // Will NOT print anything
} else {
    echo 'not null'; // Correctly identifies $a as an empty string, not NULL
}

In this corrected block, $a remains a string. Even though the value is empty, the type is string. The strict comparison fails because string is not identical to NULL, preventing the erroneous conversion that occurred with ==. This distinction becomes even more critical when dealing with database results or API responses where data types might be inconsistent.

Practical Implications in Laravel

If you are working within the Laravel ecosystem, this distinction is vital for handling form inputs and request parameters. Laravel's request() helper often returns null for missing fields but an empty string '' for fields explicitly submitted as empty.

Consider a login form where the password field might be submitted as an empty string by a user attempting to bypass validation. If your code relies on loose comparison:

if ($password == null) { ... }

You might accidentally treat a valid but empty submission the same as a missing parameter, or conversely, fail to catch specific type mismatches depending on how the upstream data was serialized.

Laravel provides utilities like old() and validation rules that handle these edge cases, but understanding the underlying PHP behavior helps you debug why a form field isn't validating as expected. For more robust handling of empty strings versus missing values in Laravel applications, refer to https://laravelcompany.com for best practices on input sanitization and strict type checking.

Debugging and Verification

When encountering this behavior in production or during debugging, it is helpful to inspect the actual types involved using var_dump() or gettype(). This reveals exactly what PHP is seeing behind the scenes.

$a = '';
var_dump($a);            // string(0) ""
var_dump($a == NULL);    // bool(true)  <- Loose comparison result
var_dump($a === NULL);   // bool(false) <- Strict comparison result

Seeing string(0) confirms that the variable is indeed a string, not an empty resource or integer. The discrepancy between the two boolean outputs highlights the exact moment where type juggling intervenes. Developers should habitually use var_dump when troubleshooting logic that behaves unexpectedly with falsy values like false, 0, '', or null.

Summary of Best Practices

The takeaway is clear: never rely on loose equality (==) when checking for NULL. Always prefer strict comparison (===) to ensure you are comparing apples to apples, not apples to oranges that have been magically converted. While PHP's flexibility allows for rapid prototyping and concise syntax, introducing a habit of strict typing early prevents technical debt that is difficult to refactor later. By adhering to this rule, you ensure your application handles edge cases like empty strings with the precision they require.

Our network