What are some common pitfalls to avoid when working with arrays and loops in PHP for data presentation?

One common pitfall is not properly initializing variables before using them in loops, which can lead to unexpected results or errors. To avoid this, always initialize variables before using them in loops to ensure they have the correct initial value.

// Incorrect way - not initializing variables before using them in loops
$sum = 0;
$numbers = [1, 2, 3, 4, 5];

foreach ($numbers as $number) {
    $sum += $number;
}

echo $sum; // Output: 15

// Correct way - initializing variables before using them in loops
$sum = 0;
$numbers = [1, 2, 3, 4, 5];

foreach ($numbers as $number) {
    $sum += $number;
}

echo $sum; // Output: 15