PhpStorm 2024.1 Help

Code Inspection: Loop does not iterate

Reports the loop statements whose bodies will execute at most once.

The inspection does not report foreach loops with a key or value used inside the loop body. Commonly, such loops are intended to perform an action only on their first iteration (for example, get the first element of an array).

In the following example, the for loop is reported by the inspection, since the loop's body is only executed once and then terminated by the break statement.

for ($myVar = 0; $myVar < 10; $myVar++) { echo $myVar; break; }

In the following example, the foreach loop is not reported by the inspection, since it is legitimately used to obtain the first element in an array.

foreach (['a', 'b', 'c'] as $item) { $firstItem = $item; break; } echo $firstItem;

Suppress an inspection in the editor

  1. Place the caret at the highlighted line and press Alt+Enter or click the Intention action icon.

  2. Click the arrow next to the inspection you want to suppress and select the necessary suppress action.

Last modified: 11 February 2024