Menu

← Back to foreach.*

Error Identifier: foreach.emptyArray

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);

/** @var array{} $items */
$items = [];

foreach ($items as $item) {
    echo $item;
}

Why is it reported? #

The expression passed to foreach is an empty array. The loop body will never execute, making it dead code. This usually indicates a logic error where the array was meant to be populated before the loop.

How to fix it #

Make sure the array is populated before iterating, or remove the dead loop:

 <?php declare(strict_types = 1);
 
-/** @var array{} $items */
-$items = [];
+$items = getItems();

 foreach ($items as $item) {
     echo $item;
 }

How to ignore this error #

You can use the identifier foreach.emptyArray to ignore this error using a comment:

// @phpstan-ignore foreach.emptyArray
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: foreach.emptyArray

Rules that report this error #

  • PHPStan\Rules\Arrays\DeadForeachRule [1]

Edit this page on GitHub

Theme
A
© 2026 PHPStan s.r.o.