What are common mistakes to avoid when using for loops in PHP?

One common mistake to avoid when using for loops in PHP is not properly initializing the loop counter variable. This can lead to unexpected behavior or errors in the loop execution. To solve this issue, always initialize the loop counter variable before starting the loop.

// Incorrect way - not initializing the loop counter variable
for ($i = 0; $i < 5; $i++) {
    echo $i;
}

// Correct way - initializing the loop counter variable
for ($i = 0; $i < 5; $i++) {
    echo $i;
}