Check if variable is true

Check if variable is true

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

Stefan Bogdanescu

Founder & Senior Architect

Evaluating Truthiness vs. Equality in PHP Conditionals

When determining whether a function call resulted in a successful operation, the instinct to explicitly compare the return value against true is common, but it often introduces unnecessary complexity and potential bugs. Consider a scenario where you are tracking analytics data:

$add_visits = add_post_meta($id, 'piwik_visits', $nb_visits, true);
if ($add_visits == true) {
    echo 'success';
}

In this snippet, the developer is asking a very specific question: "Did add_post_meta return the boolean value true?" While logically sound in isolation, this approach fails to account for other valid truthy values that PHP functions might return. Many database or ORM operations, for instance, return an integer ID upon success rather than a boolean flag. If add_post_meta were designed to return the ID of the new row on success and false on failure, checking == true would incorrectly fail even when the operation succeeded.

The alternative pattern removes the explicit comparison:

$add_visits = add_post_meta($id, 'piwik_visits', $nb_visits, true);
if ($add_visits) {
    echo 'success';
}

This is known as checking for "truthiness." In PHP, a variable evaluates to false if it is false, 0, an empty string "", null, or an empty array []. Any other value—including positive integers, non-empty strings, and objects—evaluates to true.

The critical distinction lies in understanding what your function actually returns. If the documentation for add_post_meta states it returns a boolean, then both snippets are functionally equivalent regarding success/failure logic. However, relying on implicit truthiness is generally safer unless you need to distinguish between specific return types (e.g., differentiating an ID of 1 from a generic success state).

The Danger of Loose Comparison (==)

Using the equality operator == alongside true creates a subtle trap known as "loose comparison." While false == true evaluates to false, the behavior can become unpredictable with type juggling. For example:

$x = 0;
$y = false;

if ($x == $y) { // Evaluates to true due to type coercion }
    echo "Equal"; 
}

if ($x === $y) { // Evaluates to false because types differ }
    echo "Strictly Equal";
}

When you write if ($add_visits == true), PHP performs type coercion. If $add_visits somehow holds a string like "success" (which is truthy), the comparison == true would coerce that string to true, and the block would execute. This masks potential logic errors where the developer expects a boolean but receives something else that happens to be truthy.

Best Practices for Boolean Logic

In modern PHP development, the idiomatic approach leans heavily toward using the strict equality operator === or simply relying on truthiness without explicit comparison.

  1. Check Truthiness: If you only care whether a value exists and is not "falsy," if ($add_visits) is the standard. It is concise and readable.
  2. Check for Specific Booleans: If the function signature guarantees a boolean return type (thanks to PHP 7+ union types or strict typing), if ($add_visits === true) provides clarity that the value isn't just "any truthy thing," but specifically true. However, this is rarely necessary unless the API returns multiple distinct positive states.
  3. Avoid == with Literals: Comparing a variable to true or false using == is almost always unnecessary. If you must check for a boolean, use === to prevent accidental type coercion from weird edge cases.

Contextual Considerations

If you are working within the Laravel ecosystem, the philosophy extends further. Laravel's conditional logic often favors truthiness checks over explicit comparisons. For instance, when handling form requests or validating attributes, we rarely write if ($request->get('role') == true); instead, we rely on the presence of a non-empty value.

Furthermore, if your application uses strict type checking (e.g., PHP 8 with declare(strict_types=1);), the behavior changes slightly. In that context, returning an integer ID from a function marked as returning bool | int requires careful handling. If you want to ensure you are strictly evaluating a boolean result in a typed environment, explicit checks become more relevant, but they must still account for the union of possible return types.

Ultimately, the choice between == true and simple truthiness depends on the contract of your function. If add_post_meta is documented to return a boolean, dropping the == true makes the code cleaner without losing functionality. If it returns an ID, you must adjust your logic entirely to check if the result is false (failure) rather than checking for truthiness directly against a string ID.

Our network