What are some common pitfalls to avoid when using a counter variable in a PHP loop?

One common pitfall to avoid when using a counter variable in a PHP loop is not properly initializing the counter variable before the loop starts. This can lead to unexpected results or errors in the loop execution. To solve this issue, always initialize the counter variable before the loop begins to ensure it starts with the correct value.

// Incorrect way: counter variable not initialized
for ($i = 0; $i < 5; $i++) {
    echo $i;
}

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