PHP 4.0 Bug #7122 Updated: reference to array element problem

From: Date: Wed, 11 Oct 2000 14:36:02 +0000
Subject: PHP 4.0 Bug #7122 Updated: reference to array element problem
Groups: php.dev 
Request: Send a blank email to php-dev+get-34674@lists.php.net to get a copy of this message
ID: 7122 Updated by: stas Reported By: gargamel@wizard-labs.org Status: Closed Bug Type: Scripting Engine problem Assigned To: Comments: That's not array's problem, that's "$x"'s problem. You assign $x to be reference to $A['b'] and then use it as a loop variable, you get it's (and thus, $A['b']-s) contents overwritten. Previous Comments: --------------------------------------------------------------------------- [2000-10-10 12:41:04] gargamel@wizard-labs.org Creating a reference to an array element prevents that particular element from being listed in a foreach-loop. Instead, the previous element in the array is processed twice. Simple PHP script to reproduce the problem: --------------- <?php $A = array( 'a' => 'A', 'b' => 'B', 'c' => 'C'); /* This works: */ foreach ($A as $y => $x) { echo "y=$y x=$xn"; } /* This breaks: */ $x = &$A['b']; foreach ($A as $y => $x) { echo "y=$y x=$xn"; } ?> ------------------- This results in the following output: --------------------- X-Powered-By: PHP/4.0.2 Content-type: text/html y=a x=A y=b x=B y=c x=C y=a x=A y=b x=A <== ERROR!! y=c x=C ------------------- Note: I think this is possibly related to bug id #6417 --------------------------------------------------------------------------- Full Bug description available at: http://bugs.php.net/?id=7122

« previous php.dev (#34674) next »