PHP IF statement for Boolean values: $var === true vs $var

PHP IF statement for Boolean values: $var === true vs $var

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

Stefan Bogdanescu

Founder & Senior Architect

PHP IF statement for Boolean values: $var === true vs $var

When deciding between if ($variable) and if ($variable === true), the choice isn't just syntactic sugar; it represents a fundamental difference in how you interpret truthiness in PHP. While both statements often result in identical execution flow for standard boolean variables, using the loose identity operator (===) introduces strict type checking that can significantly alter behavior depending on what your variable actually holds at runtime.

The bare if ($variable) construct relies on PHP's concept of "truthy" values. In this context, any non-empty string, non-zero integer, or even an object will evaluate to true. This is useful when you want to treat a collection or a configuration array as valid regardless of its specific internal state. However, if your goal is strictly to verify that a flag has been explicitly set to the boolean true, relying on loose evaluation can lead to subtle bugs. For instance, if $variable contains an empty string "", the condition if ($variable) evaluates to false, which is correct. But if it contains the integer 0, it also evaluates to false. The real danger arises when dealing with objects or strings that happen to be truthy but are not the boolean true you intended to check for.

Using $variable === true adds a strict type constraint. This operator checks two things: the value and the data type. If $variable is the integer 1, the expression $variable === true evaluates to false because, despite being truthy, it is an integer, not a boolean. Conversely, if $variable is set to true or TRUE, both are treated as equivalent in PHP's loose comparison, but with ===, they must match exactly. This distinction becomes critical when dealing with legacy codebases where booleans might be cast from integers or strings unintentionally.

In modern PHP development, the loose check if ($variable) is generally preferred for control flow unless you are specifically validating a type. It allows for more flexible logic without imposing unnecessary type barriers. For example, checking if an array has data is often better expressed as if ($data) rather than if ($data === true), because the latter would fail even if $data contains a list of items.

However, there are scenarios where strict comparison is mandatory. Consider a scenario involving configuration flags stored in a database or passed via API requests. If your application expects a specific boolean flag to be present, using === true prevents logic errors caused by implicit type casting from string "1" or integer 1. This is particularly relevant when integrating with external systems that might send numeric representations of booleans instead of the native boolean type.

If you are working within the Laravel ecosystem, this distinction often surfaces when handling form requests or validation rules. While Laravel's validation helpers abstract much of this complexity, understanding the underlying behavior ensures you can write robust custom validators. For instance, if you need to ensure a specific setting is strictly enabled and not just "truthy," strict comparison guarantees that only the boolean true passes the check.

Ultimately, the decision rests on what you are trying to assert. If you want to know if something exists or has a value, use if ($variable). If you need to confirm that a specific boolean flag is set and nothing else will suffice, use if ($variable === true). The performance difference between these two operations is negligible in modern PHP versions, so the choice should be driven by semantic clarity and type safety rather than execution speed.

Our network