website/errors/booleanAnd.leftAlwaysFalse.md
<?php declare(strict_types = 1);
function doFoo(int $i): void
{
$zero = 0;
if ($zero && $i) {
// never reached
}
}
The left side of the && (boolean AND) expression always evaluates to false. Because && uses short-circuit evaluation, when the left operand is always falsy, the right operand is never evaluated, and the entire expression is always false. This means the condition body will never be executed, indicating a logic error or redundant code.
In the example above, $zero is always 0, which is falsy in PHP, so $zero && $i is always false.
Remove the redundant condition if the code inside should never execute:
<?php declare(strict_types = 1);
function doFoo(int $i): void
{
- $zero = 0;
- if ($zero && $i) {
- // never reached
- }
}
Or fix the logic to check the correct variable:
<?php declare(strict_types = 1);
function doFoo(int $i): void
{
- $zero = 0;
- if ($zero && $i) {
+ if ($i && $i > 0) {
// now depends on actual input
}
}