website/errors/if.alwaysFalse.md
<?php declare(strict_types = 1);
function doFoo(): void
{
$x = 0;
if ($x) {
echo 'never reached';
}
}
The if condition is always false based on the types and values PHPStan has inferred at that point in the code. The body of the if statement will never execute, making it dead code. This usually points to a logic error or redundant check.
In the example above, $x is always 0 (falsy), so the if body is unreachable.
Review the surrounding logic and either remove the dead branch or fix the condition:
<?php declare(strict_types = 1);
function doFoo(): void
{
- $x = 0;
- if ($x) {
- echo 'never reached';
- }
+ echo 'always reached';
}
Or fix the variable assignment so the condition can be true:
<?php declare(strict_types = 1);
-function doFoo(): void
+function doFoo(int $x): void
{
- $x = 0;
if ($x) {
- echo 'never reached';
+ echo 'nonzero';
}
}