How can the use of the same variable name in nested for loops impact the functionality of PHP code?
Using the same variable name in nested for loops can lead to conflicts and unexpected behavior in PHP code. To avoid this issue, it is recommended to use different variable names for each loop to prevent any unintended overwriting of values.
// Incorrect code with the same variable name in nested loops
for ($i = 0; $i < 3; $i++) {
for ($i = 0; $i < 3; $i++) {
echo $i;
}
}
// Corrected code with different variable names in nested loops
for ($i = 0; $i < 3; $i++) {
for ($j = 0; $j < 3; $j++) {
echo $i . $j;
}
}