website/errors/match.alwaysFalse.md
<?php declare(strict_types = 1);
/**
* @param 1|2|3 $i
*/
function doFoo(int $i): void
{
match ($i) {
'foo' => 'matched foo', // error: Match arm comparison between 1|2|3 and 'foo' is always false.
default => 'default',
};
}
The match expression uses strict comparison (===) to evaluate each arm. When the subject type and the arm condition type have no overlap, the comparison will always evaluate to false, meaning the arm can never be matched. In the example above, the parameter $i is typed as 1|2|3 (integers), so comparing it against the string 'foo' will never succeed.
Remove the unreachable match arm or fix the arm condition to use a value that can actually match the subject.
/**
* @param 1|2|3 $i
*/
function doFoo(int $i): void
{
match ($i) {
- 'foo' => 'matched foo',
+ 1 => 'matched one',
default => 'default',
};
}