Menu

Error Identifier: if.alwaysTrue

← Back to if.*

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 doFoo(): void
{
	$x = 5;
	if ($x) {
		echo 'always reached';
	}
}

Why is it reported? #

The if condition is always true based on the types and values PHPStan has inferred at that point in the code. This means the if branch will always execute, making the condition redundant. This usually points to an unnecessary check, a logic error, or a misunderstanding of the types involved.

In the example above, $x is always 5 (truthy), so the condition is always satisfied.

How to fix it #

Remove the redundant condition if the check is unnecessary:

 <?php declare(strict_types = 1);
 
 function doFoo(): void
 {
-	$x = 5;
-	if ($x) {
-		echo 'always reached';
-	}
+	echo 'always reached';
 }

If the condition was meant to distinguish between different cases, fix the condition to check what was actually intended:

 <?php declare(strict_types = 1);
 
-function doFoo(): void
+function doFoo(int $x): void
 {
-	$x = 5;
-	if ($x) {
-		echo 'always reached';
+	if ($x > 0) {
+		echo 'positive';
 	}
 }

How to ignore this error #

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

// @phpstan-ignore if.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: if.alwaysTrue

Rules that report this error #

  • PHPStan\Rules\Comparison\IfConstantConditionRule [1]
Theme
A
© 2026 PHPStan s.r.o.