What are some common pitfalls when using while loops in PHP and how can they be avoided?

One common pitfall when using while loops in PHP is forgetting to update the loop control variable inside the loop, leading to an infinite loop. To avoid this, always make sure to update the loop control variable within the loop body. Example:

// Incorrect - missing loop control variable update
$counter = 0;
while ($counter < 5) {
    echo $counter;
    // missing $counter++;
}

// Correct - updating loop control variable
$counter = 0;
while ($counter < 5) {
    echo $counter;
    $counter++;
}