|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2003-06-10 17:20 UTC] young at sl dot com dot ua
Source code N1:
-----
foreach ($sqldata as $row) {
$item = $row;
print_r($item);
}
-----
Result 1:
Array
(
[DURATION] => 7
)
Array
(
[DURATION] => 230
)
Array
(
[DURATION] => 230
)
-----
Source code 2:
-----
foreach ($sqldata as $item) {
print_r($item);
}
-----
Result 2:
-----
Array
(
[DURATION] => 7
)
Array
(
[DURATION] => 230
)
Array
(
[DURATION] => 50
)
------
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Wed Nov 19 12:00:02 2025 UTC |
Sample code to make this bug: <?php $arr = array(1, 2, 3, 4, 5); $sizeOf = sizeof($arr); for ($i = 0; $i < $sizeOf; $i++) { $row =& $arr[$i]; $row = $row * $row; } foreach ($arr as $row) { echo $row."<br>"; } ?> Return 1 4 9 16 16Yes; more evidence: <?php $arr = array(1, 2, 3, 4, 5); $sizeOf = sizeof($arr); for ($i = 0; $i < $sizeOf; $i++) { $row = &$arr[$i]; } foreach ($arr as $row) { echo $row."<br>"; } ?> Output: 1<br>2<br>3<br>4<br>4<br> <?php $arr = array(1, 2, 3, 4, 5); $sizeOf = sizeof($arr); for ($i = 0; $i < $sizeOf; $i++) { $row = &$arr[$i]; } foreach ($arr as $x) { echo $x."<br>"; } ?> Output: 1<br>2<br>3<br>4<br>5<br>This bug has been fixed in the documentation's XML sources. Since the online and downloadable versions of the documentation need some time to get updated, we would like to ask you to be a bit patient. Thank you for the report, and for helping us make our documentation better. Note: "If you assign a value to variable with references in the foreach statement, references are modified too." + example: <?php $ref = 0; $row =& $ref; foreach (array(1, 2, 3) as $row) { // do something } echo $ref; // 3 - last element of the iterated array ?>