PHP array printing using a loop

PHP array printing using a loop

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Printing PHP Arrays Using a Loop

When you know the exact length of an array, iterating through it requires accessing elements by their integer indices. In modern PHP, this is most efficiently achieved using a for loop or a foreach loop that leverages the internal counter provided by the language engine. Understanding the mechanics behind these loops ensures you don't run into off-by-one errors or performance bottlenecks in larger applications.

If you are working with numeric indices and have access to the array's length, the classic for loop offers explicit control over the iteration process. You retrieve the total count using count($array) or rely on the static property $array['length'] if it exists (though standard arrays typically use count()). The syntax initializes a counter variable, checks it against the limit before each iteration, and increments it after processing the current element.

$items = [10, 20, 30, 40, 50];
$length = count($items);

for ($i = 0; $i < $length; $i++) {
    echo "Index {$i}: {$items[$i]}<br>";
}

This approach is robust and readable. However, be mindful of the separation between the loop counter ($i) and the array access ($items[$i]). While this works perfectly for standard indexed arrays, it introduces a slight cognitive overhead compared to PHP's iterator-based constructs. If you ever need to modify the index itself or perform complex logic based on the position, the explicit for loop remains your best tool.

For most practical scenarios where you simply need to traverse the values, PHP provides a cleaner abstraction through the foreach loop. Even though you know the length of the array, using foreach eliminates the need to manually manage an index variable. The engine automatically handles the internal pointer, fetching the current value and moving to the next element until the end is reached.

$items = [10, 20, 30, 40, 50];

foreach ($items as $value) {
    echo "Value: {$value}<br>";
}

This syntax is generally preferred in Laravel and modern PHP development because it reduces the chance of accidental index modification. You cannot accidentally skip an element or go out of bounds because the underlying implementation guarantees a sequential traversal. The trade-off here is that you lose direct access to the numeric index unless you explicitly enable it using as $key => $value.

$items = [10, 20, 30, 40, 50];

foreach ($items as $index => $value) {
    // Accessing both index and value
    echo "Index {$index}: Value {$value}<br>";
}

A common pitfall when using for loops with arrays is modifying the array while iterating. If you push new elements into $items inside a loop that relies on count($items) for its termination condition, the loop may run indefinitely or crash depending on how PHP evaluates the count. This happens because count() returns the current length at every iteration check, effectively changing the target limit dynamically.

// Dangerous pattern: modifying array during iteration
$items = [1, 2, 3];
$count = count($items); // Captured once? No, if re-evaluated inside condition
for ($i = 0; $i < count($items); $i++) { // count() called every time!
    echo $items[$i] . " ";
    array_push($items, $i * 10); // Modifies length while loop checks condition
}

In this specific example, count($items) is evaluated on every iteration of the loop header. If you push a new item inside the loop body, the length increases, potentially causing an infinite loop or accessing uninitialized keys if logic isn't perfectly aligned. Always ensure the array remains static during iteration unless you are specifically implementing advanced algorithms that require dynamic resizing.

For developers working within the Laravel ecosystem, these native PHP constructs remain fully supported and performant. There is no need to switch to Eloquent collections or helper methods for simple array printing tasks; sticking to standard loops keeps your codebase lightweight and compatible with older PHP versions if necessary. If you are looking for more advanced collection manipulation techniques or need to integrate array processing into a broader Laravel workflow, resources like https://laravelcompany.com can offer deeper insights into optimizing backend logic beyond basic iteration.

Our network