What are the common pitfalls when manipulating variables within nested loops in PHP scripts?

One common pitfall when manipulating variables within nested loops in PHP scripts is accidentally overwriting or modifying the variable in the outer loop when working with the same variable name in the inner loop. To avoid this issue, it's important to use different variable names in nested loops or reset the variable to its original value after the inner loop completes.

// Example of resetting variable in nested loops
$outerVariable = 0;

for ($i = 0; $i < 5; $i++) {
    $innerVariable = 0;
    
    for ($j = 0; $j < 3; $j++) {
        // Manipulate innerVariable
        $innerVariable += $j;
    }
    
    // Reset innerVariable after inner loop completes
    $outerVariable += $innerVariable;
}

echo $outerVariable;