note 107076 added to control-structures.foreach
| From: | z666zz666z at terra dot es | Date: | Thu, 05 Jan 2012 09:37:49 +0000 |
| Subject: | note 107076 added to control-structures.foreach | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-185370@lists.php.net to get a copy of this message | ||
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)
?>
----
Server IP: 192.41.42.26
Probable Submitter: 83.42.126.86
----
Manual Page -- http://www.php.net/manual/en/control-structures.foreach.php
Edit -- https://master.php.net/note/edit/107076
Del: integrated -- https://master.php.net/note/delete/107076/integrated
Del: useless -- https://master.php.net/note/delete/107076/useless
Del: bad code -- https://master.php.net/note/delete/107076/bad+code
Del: spam -- https://master.php.net/note/delete/107076/spam
Del: non-english -- https://master.php.net/note/delete/107076/non-english
Del: in docs -- https://master.php.net/note/delete/107076/in+docs
Del: other reasons-- https://master.php.net/note/delete/107076
Reject -- https://master.php.net/note/reject/107076
Search -- https://master.php.net/manage/user-notes.php