Why a function checking if a string is empty always returns true?

Why a function checking if a string is empty always returns true?

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

Stefan Bogdanescu

Founder & Senior Architect

The Silent Failure of $strTemp in PHP String Validation

You are passing an empty string into your isNotEmpty function, yet the logic inside the function evaluates as if the string contains data. This behavior is a classic case of a syntax error masquerading as a logic bug. Specifically, the culprit lies in this line:

if(strTemp != '') 

In PHP, variable names are case-sensitive. You defined your temporary variable as $strTemp (with a dollar sign), but inside the condition, you referenced strTemp without the dollar sign. In PHP 5 and earlier, omitting the $ in an array or object context might sometimes be implicitly resolved, but for scalar variables like strings, this results in a parse error or, depending on your specific PHP version and configuration, it is treated as a literal string comparison against the variable name itself rather than its value.

However, even if you assume a hypothetical environment where strTemp was somehow interpreted as $strTemp, there is another critical flaw preventing your validation from working as intended: variable scoping within control structures.

The Variable Scope Trap

The most immediate issue that prevents the else block from executing when an empty string is passed is how the code handles the variable name. If you look closely at the provided source material, the line causing the failure is likely a typo where the $ was dropped entirely in the condition check. But let's assume for a moment the code was written correctly as if ($strTemp != '').

If the code were syntactically perfect, your logic would hold up for empty strings unless you are running into an issue with how PHP handles the variable definition or if there is a hidden whitespace issue introduced by trim() that isn't being accounted for. However, the most common reason this specific snippet fails in real-world scenarios involves the distinction between checking for an empty string versus checking for a null value, or simply a syntax error preventing execution.

Let's reconstruct the function with the necessary corrections to ensure it behaves predictably:

function isNotEmpty($input) 
{
    if (empty(trim($input))) {
        return false;
    }
    
    return true;
}

The empty() superglobal is often the preferred method for this specific check because it handles multiple "falsy" states in one go. An empty string '', a string containing only whitespace, null, false, and 0 all return true when passed to empty(). Since you are using trim() first, any whitespace-only strings become empty strings, which empty() correctly identifies.

Why the Original Logic Failed

In your original code snippet, if the line if(strTemp != '') was intended to be if($strTemp != ''), the logic itself is sound for checking length. However, if you are seeing the else block execute when it shouldn't (or vice versa), check your PHP version. In older versions of PHP, loose comparison operators (!=) can lead to unexpected type juggling, though in this specific context, it usually just compares the string value to an empty string.

The real lesson here is about strictness. While != works for many cases, relying on empty() combined with trim() reduces cognitive load and minimizes edge-case bugs. For instance, if your input comes from a GET parameter that defaults to an empty string but might sometimes be missing entirely (null), empty() catches both scenarios seamlessly.

Practical Implementation in Modern PHP

When building robust validation layers, especially in frameworks like Laravel, relying on custom functions for basic existence checks can sometimes obscure the intent of your code. The Laravel validation syntax, for example, provides a built-in required and filled rule that handles exactly this logic without needing a separate helper function.

If you are maintaining legacy PHP code or working outside a framework where these helpers don't exist, always ensure your variable references include the $ symbol. A missing dollar sign doesn't just skip a variable; it fundamentally changes what is being evaluated. In strict modes enabled via error_reporting(E_ALL), this would throw a fatal error immediately, stopping execution before your validation logic could even run.

By correcting the syntax and potentially simplifying the logic using empty(), you ensure that your phone number validation (or whatever field you are checking) behaves consistently regardless of whether the input is an empty string, null, or whitespace-only.

Our network