website/errors/notEqual.invalid.md
<?php declare(strict_types = 1);
function doFoo(stdClass $obj, int $number): void
{
$result = $obj != $number; // error: Comparison operation "!=" between stdClass and int results in an error.
}
PHP does not support loose comparison (!=) between certain type combinations. When comparing an object with a scalar type like int using !=, PHP cannot perform a meaningful comparison and will produce an error. The operation has no well-defined behavior for these types.
Compare values of the same or compatible types, or extract a comparable value from the object first.
function doFoo(stdClass $obj, int $number): void
{
- $result = $obj != $number;
+ $result = $obj->value !== $number;
}