Error Identifier: equal.alwaysTrue
Every error reported by PHPStan has an error identifier. Here’s a list of all error identifiers. In PHPStan Pro you can see the error identifier next to each error and filter errors by their identifiers.
Code example #
<?php declare(strict_types = 1);
function doFoo(): void
{
if (0 == '0') { // error: Loose comparison using == between 0 and '0' will always evaluate to true.
// ...
}
}
Why is it reported? #
A loose comparison using == is always true based on the types of the compared values. This means the condition is redundant – it will never evaluate to false. This usually indicates a logic error, a misunderstanding of PHP’s type juggling rules, or code that has become dead after refactoring.
PHP’s loose comparison (==) performs type coercion before comparing, which can lead to surprising results. For example, 0 == '0' is always true because PHP converts the string to an integer for comparison.
How to fix it #
If the comparison is intentional, use strict comparison (===) which does not perform type coercion:
<?php declare(strict_types = 1);
function doFoo(): void
{
- if (0 == '0') {
+ if (0 === 0) {
// ...
}
}
If the condition is always true, the conditional branch is unnecessary and can be simplified:
<?php declare(strict_types = 1);
function doFoo(): void
{
- if (0 == '0') {
- // ...
- }
+ // ...
}
How to ignore this error #
You can use the identifier equal.alwaysTrue to ignore this error using a comment:
// @phpstan-ignore equal.alwaysTrue
codeThatProducesTheError();
You can also use only the identifier key to ignore all errors of the same type in your configuration file in the ignoreErrors parameter:
parameters:
ignoreErrors:
-
identifier: equal.alwaysTrue
Rules that report this error #
- PHPStan\Rules\Comparison\ConstantLooseComparisonRule [1]