Error Identifier: notEqual.notAllowed
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);
$result = 123 != 456; // error: Loose comparison via "!=" is not allowed.
Why is it reported? #
Loose comparison (!=) uses PHP’s type juggling rules to compare values, which can produce surprising and hard-to-predict results. For example, "0" != "" evaluates to true, but 0 != "" evaluates to false. These implicit type conversions are a common source of bugs.
This error is reported by phpstan-strict-rules.
How to fix it #
Use strict comparison (!==) instead, which compares both the value and the type without implicit conversion.
-$result = 123 != 456;
+$result = 123 !== 456;
If the values have different types and a comparison is still needed, cast one operand explicitly to make the intent clear.
-if ($userInput != 0) {
+if ((int) $userInput !== 0) {
// ...
}
How to ignore this error #
You can use the identifier notEqual.notAllowed to ignore this error using a comment:
// @phpstan-ignore notEqual.notAllowed
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: notEqual.notAllowed
Rules that report this error #
- PHPStan\Rules\DisallowedConstructs\DisallowedLooseComparisonRule [1] phpstan/phpstan-strict-rules