note 107076 deleted from control-structures.foreach by danbrown
| From: | danbrown@php.net | Date: | Fri, 06 Jan 2012 19:16:51 +0000 |
| Subject: | note 107076 deleted from control-structures.foreach by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-185411@lists.php.net to get a copy of this message | ||
Note Submitter: z666zz666z at terra dot es
----
Warning!!! On PHP4 foreach does not acept the & of "... as &$ ..."
So, this code will only work on PHP5, not on PHP4:
<?php
$arr = array(1, 2, 3, 4);
foreach ($arr as &$value) {
$value = $value * 2;
}
// $arr is now array(2, 4, 6, 8)
unset($value); // break the reference with the last element
?>
On PHP4 inside foreach can not be the &$ to pass it by reference, must be done like this:
<?php
$arr = array(1, 2, 3, 4);
foreach ($arr as $index => $value) {
$arr[$index] = $arr[$index] * 2;
}unset($index,$value);
// $arr is now array(2, 4, 6, 8)
?>