|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2010-02-15 04:47 UTC] timminn at gmail dot com
Description:
------------
if AN ARRAY WITHIN AN OBJECT is used in "a straight manner" in the foreach() loop, with the references to its values[poor English, see the SECOND foreach() in the submitted code to understand I mean],
the last value in the array will be REPACED with the second last one ACCIDENTALLY, WITHOUT ANY ASSIGNMENT OPERATION.
Reproduce code:
---------------
<?php
class myc{ public $a; }
$o3->a = Array( 11, 22 );
//loop ONE
foreach( $o3->a as $k => $v )
echo $v. ' '; //EXPECT 11 22 , CORRECT OUTPUT
echo "\n=========\n";
//loop TWO notice the reference & sign for $v
foreach( $o3->a as $k => & $v )
echo $v. ' '; //EXPECT 11 22 , CORRECT OUTPUT
echo "\n=========\n";
//loop THREE
foreach( $o3->a as $k => $v )
echo $v. ' '; //EXPECT 11 22 , BUT WRONG OUTPUT: 11 11
echo "\n=========\n";
?>
Expected result:
----------------
11 22
11 22
11 22
Actual result:
--------------
11 22
11 22
11 11
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Wed Dec 17 20:00:01 2025 UTC |
the bug occurs ONLY WHEN: MUST BE used in a STRAIGHT MANNER as ARRAY IN OBJECT, as shown in the sbmitted code, in other words, if we use $sa = $o3->a; and foreach( $sa as $k => & $v ){ ... } the bug will not occur