Menu

← Back to while.*

Error Identifier: while.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 doFoo(): void
{
	while (true) {

	}
}

Why is it reported? #

The condition of the while loop always evaluates to true, and the loop does not contain a break statement or other exit point. This creates an infinite loop that will never terminate, which is almost always a bug.

If the enclosing function has a never return type, this error is not reported because the infinite loop is intentional.

How to fix it #

Add an exit condition to the loop:

-function doFoo(): void
+function doFoo(bool $running): void
 {
-	while (true) {
-
+	while ($running) {
+		// do something
+		$running = false;
 	}
 }

Or add a break statement inside the loop body:

 function doFoo(): void
 {
 	while (true) {
-
+		// do something
+		if ($condition) {
+			break;
+		}
 	}
 }

If the infinite loop is intentional (e.g., for a long-running daemon), declare the function return type as never:

-function doFoo(): void
+function doFoo(): never
 {
 	while (true) {
-
+		// process requests
 	}
 }

How to ignore this error #

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

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

Rules that report this error #

  • PHPStan\Rules\Comparison\WhileLoopAlwaysTrueConditionRule [1]

Edit this page on GitHub

Theme
A
© 2026 PHPStan s.r.o.