Bug #66859 [Opn->Nab]: Variable re-use in for loop causes corruption

From: Date: Sun, 09 Mar 2014 06:48:32 +0000
Subject: Bug #66859 [Opn->Nab]: Variable re-use in for loop causes corruption
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-184630@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=66859&edit=1

 ID:                 66859
 Updated by:         requinix@php.net
 Reported by:        jake_hotson at hotmail dot com
 Summary:            Variable re-use in for loop causes corruption
-Status:             Open
+Status:             Not a bug
 Type:               Bug
 Package:            Scripting Engine problem
 Operating System:   Win XP Pro
 PHP Version:        Irrelevant
 Block user comment: N
 Private report:     N

 New Comment:

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

References. After the first loop $fruit is still a reference and that means attempting to change its
value will affect whatever it was a reference to as well. In this situation you'll experience
that as showing "<key> is <previous key>" in the last iteration.

unset($fruit) after the first loop.


Previous Comments:
------------------------------------------------------------------------
[2014-03-09 06:14:04] jake_hotson at hotmail dot com

Description:
------------
PHP ver 5.4.8.  Unable to test in 5.5.10 as seems incompatible with Apache 2.4.7.

Script below says that orange is an apple, but if the first foreach is commented out it correctly
says that it is fruity.

Test script:
---------------
        $fruits = array(
          'apple' => 'fruity',
          'orange' => 'fruity',
        );
        foreach ($fruits as &$fruit) {
        }
        foreach ($fruits as $fruit => $description) {
          echo "$fruit is $description<br />\n";
        }


Expected result:
----------------
apple is fruity
orange is fruity

Actual result:
--------------
apple is fruity
orange is apple


------------------------------------------------------------------------



-- 
Edit this bug report at https://bugs.php.net/bug.php?id=66859&edit=1


Thread (3 messages)

« previous php.bugs (#184630) next »