0

My script is:

$arr=array('A','B');
foreach($arr as &$val){
    $val=str_replace(array('B'),array('C'),substr($val,0,1));
}
print_r($arr);
foreach($arr as $key=>$val)echo $key.'=>'.$val.'|||||';
echo phpversion();

When i run it responce look like below:

Array
(
    [0] => A
    [1] => C
)
0=>A|||||1=>A|||||5.6.16

I think responce must be as 0=>A|||||1=>C||||| but it is not. I can't find reason. Is this a PHP bug or is my script needs a fix?

MERT DOĞAN
  • 2,864
  • 26
  • 28
  • 1
    change the `$val` to `$value` or whatever since both the `$val` is in the same scope, the second `$val` will be refer to the first `$val`, example : https://3v4l.org/p9sFQ – Andrew Feb 23 '16 at 14:28
  • But this seems like a bug; doesn't it? – MERT DOĞAN Feb 24 '16 at 11:50

0 Answers0