website/errors/notIdentical.alwaysTrue.md
<?php declare(strict_types = 1);
function doFoo(int $i, string $s): void
{
if ($i !== $s) { // error: Strict comparison using !== between int and string will always evaluate to true.
// ...
}
}
The strict not-identical operator (!==) compares both value and type. When the two operands can never be of the same type, the comparison will always evaluate to true, making the condition meaningless. This usually indicates a logic error -- the code is checking something that can never be false.
This error can be turned off for the last condition in an if/elseif chain via the reportAlwaysTrueInLastCondition option.
Compare values of compatible types, or remove the unnecessary condition.
-function doFoo(int $i, string $s): void
+function doFoo(int $i, int $j): void
{
- if ($i !== $s) {
+ if ($i !== $j) {
// ...
}
}
If comparing different representations of the same logical value, convert one operand first.
function doFoo(int $i, string $s): void
{
- if ($i !== $s) {
+ if ($i !== (int) $s) {
// ...
}
}