Menu

← Back to elseif.*

Error Identifier: elseif.alwaysTrue

Every error reported by PHPStan has an error identifier. Here’s a list of all error identifiers. In PHPStan Pro you can see the error identifier next to each error and filter errors by their identifiers.

Code example #

<?php declare(strict_types = 1);

function classify(int $value): string
{
	if ($value > 0) {
		return 'positive';
	} elseif (is_int($value)) {
		return 'zero or negative';
	}

	return 'unknown';
}

Why is it reported? #

The condition in the elseif branch always evaluates to true. This means the elseif is equivalent to a plain else, and any code after it is unreachable. This typically indicates that the condition is redundant or that the logic does not match the developer’s intent.

In the example above, $value is typed as int, so is_int($value) is always true regardless of the if branch above.

How to fix it #

Replace the elseif with else if the condition is truly unnecessary:

 <?php declare(strict_types = 1);
 
 function classify(int $value): string
 {
 	if ($value > 0) {
 		return 'positive';
-	} elseif (is_int($value)) {
+	} else {
 		return 'zero or negative';
 	}
-
-	return 'unknown';
 }

Or write a more specific condition if different cases need to be distinguished:

 <?php declare(strict_types = 1);
 
 function classify(int $value): string
 {
 	if ($value > 0) {
 		return 'positive';
-	} elseif (is_int($value)) {
-		return 'zero or negative';
+	} elseif ($value === 0) {
+		return 'zero';
+	} else {
+		return 'negative';
 	}
-
-	return 'unknown';
 }

How to ignore this error #

You can use the identifier elseif.alwaysTrue to ignore this error using a comment:

// @phpstan-ignore elseif.alwaysTrue
codeThatProducesTheError();

You can also use only the identifier key to ignore all errors of the same type in your configuration file in the ignoreErrors parameter:

parameters:
	ignoreErrors:
		-
			identifier: elseif.alwaysTrue

Rules that report this error #

  • PHPStan\Rules\Comparison\ElseIfConstantConditionRule [1]

Edit this page on GitHub

Theme
A
© 2026 PHPStan s.r.o.