I have following code as below,
<?php
$a = [1,2,3];
foreach($a as &$val) {
$val = $val + 1;
}
foreach($a as $val) {
$val = $val - 1;
}
var_dump($a);
// output 2,3,1
?>
I got output 2,3,1 as final array instead of 2,3,4 and i can't understand how php is interpreting this code, Can anyone help me to understand how the things is going here?
2,3,4and be unaffected by the secondedforeach.foreachby reference means searching for trouble. Don't do it!foreachin a big red warning box.