What potential pitfalls should be avoided when using loops in PHP templates?

One potential pitfall to avoid when using loops in PHP templates is not properly initializing variables before the loop. This can lead to unexpected behavior or errors when trying to access the variables within the loop. To solve this issue, make sure to initialize any variables that will be used within the loop before the loop starts.

<?php
// Initialize variables before the loop
$items = ['Item 1', 'Item 2', 'Item 3'];

// Loop through the items
foreach ($items as $item) {
    echo $item . '<br>';
}
?>