PHP: references in foreach

raw

code.php

1
2
3
4
5
6
7
8
9
10
11
<?php
$a = ['a1','a2'];
var_dump($a);
 
foreach ($a as &$val) {}
var_dump($a);
 
$b = ['b3'];
foreach ($b as $val) {}
var_dump($a);
?>
raw

output.txt

array(2) {
  [0]=>
  string(2) "a1"
  [1]=>
  string(2) "a2"
}
array(2) {
  [0]=>
  string(2) "a1"
  [1]=>
  &string(2) "a2"
}
array(2) {
  [0]=>
  string(2) "a1"
  [1]=>
  &string(2) "b3"
}
raw

reason.rst

Christian Weiske Christian Weiske
owner

History