php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #24118 Error while using foreach
Submitted: 2003-06-10 17:20 UTC Modified: 2004-08-07 12:05 UTC
Votes:3
Avg. Score:3.3 ± 1.2
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: young at sl dot com dot ua Assigned:
Status: Closed Package: Documentation problem
PHP Version: Irrelevant OS: *
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: young at sl dot com dot ua
New email:
PHP Version: OS:

 

 [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
)
------

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2003-06-10 17:24 UTC] elmicha@php.net
Not enough information was provided for us to be able
to handle this bug. Please re-read the instructions at
http://bugs.php.net/how-to-report.php

If you can provide more information, feel free to add it
to this bug and change the status back to "Open".

Thank you for your interest in PHP.

 [2003-06-10 17:32 UTC] young at sl dot com dot ua
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
16
 [2003-06-10 17:56 UTC] sniper@php.net
Zeev, here is first one I verified with both 4.3.3-dev and 5.0.0-dev. Have fun fixing this one. :)

 [2003-06-10 18:15 UTC] elmicha@php.net
Yes; 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>
 [2003-06-11 02:59 UTC] zeev@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

Taking the reproducible example:

<?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>";
}
?>

You shouldn't be using the reference variable that you used previously in the foreach loop, since every iteration will assign the value to both $row, and any other references it's attached to, in this case, $arr[4].  This is why you get the last iterated value ($arr[3]) in $arr[4], when you get to it.


 [2003-06-11 05:22 UTC] sniper@php.net
Make this a documentation problem..I couldn't find this explanation there. :)

 [2004-08-07 12:05 UTC] vrana@php.net
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
?>

 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Jun 15 15:01:39 2024 UTC