What are some best practices for incrementing variables in PHP loops to avoid errors or unexpected behavior?

When incrementing variables in PHP loops, it's important to ensure that the increment operation is properly handled to avoid errors or unexpected behavior. One common mistake is not incrementing the variable within the loop, leading to an infinite loop or incorrect results. To avoid this, always remember to increment the variable inside the loop based on the desired logic.

// Incorrect way of incrementing variable in a loop
$i = 0;
while ($i < 5) {
    // Missing increment of $i
}

// Correct way of incrementing variable in a loop
$i = 0;
while ($i < 5) {
    // Increment $i by 1 in each iteration
    $i++;
}