What are common pitfalls when using PHP variables in loops?

Common pitfalls when using PHP variables in loops include variable scope issues, where variables defined outside the loop may not be accessible inside the loop, and unintentional variable overwriting, where a variable is redefined within the loop causing unexpected behavior. To avoid these pitfalls, it's important to properly initialize variables before the loop and ensure that variables are not inadvertently overwritten within the loop.

// Example of initializing variables before the loop to avoid scope issues
$sum = 0;
for ($i = 1; $i <= 10; $i++) {
    $sum += $i;
}
echo $sum;