Bug #54408 [NEW]: Unexpected behavior when using by-reference iteration

From: Date: Mon, 28 Mar 2011 12:35:25 +0000
Subject: Bug #54408 [NEW]: Unexpected behavior when using by-reference iteration
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-159808@lists.php.net to get a copy of this message
From:             
Operating system: Win 7 x64, 2.6.18
PHP version:      5.3.6
Package:          Arrays related
Bug Type:         Bug
Bug description:Unexpected behavior when using by-reference iteration

Description:
------------
I was sure that arrays are passed to functions by value, not by reference.
It looks like after by-reference iteration changes array not only isnide
foreach statement, but array remains changed even after iteration.

Test script:
---------------
<?php
$a = array(1,2,3,4);

function myf($a,$k){
    $a[$k] = 9;
}
foreach ($a as $k => &$v) {
    myf($a, $k);
}
//myf($a, 3);
var_dump($a);
die();

Expected result:
----------------
array
  0 => int 1
  1 => int 2
  2 => int 3  
  3 => int 4

Actual result:
--------------
array
  0 => int 9
  1 => int 9
  2 => int 9
  3 => &int 9

-- 
Edit bug report at http://bugs.php.net/bug.php?id=54408&edit=1
-- 
Try a snapshot (PHP 5.2):            http://bugs.php.net/fix.php?id=54408&r=trysnapshot52
Try a snapshot (PHP 5.3):            http://bugs.php.net/fix.php?id=54408&r=trysnapshot53
Try a snapshot (trunk):              http://bugs.php.net/fix.php?id=54408&r=trysnapshottrunk
Fixed in SVN:                        http://bugs.php.net/fix.php?id=54408&r=fixed
Fixed in SVN and need be documented: http://bugs.php.net/fix.php?id=54408&r=needdocs
Fixed in release:                    http://bugs.php.net/fix.php?id=54408&r=alreadyfixed
Need backtrace:                      http://bugs.php.net/fix.php?id=54408&r=needtrace
Need Reproduce Script:               http://bugs.php.net/fix.php?id=54408&r=needscript
Try newer version:                   http://bugs.php.net/fix.php?id=54408&r=oldversion
Not developer issue:                 http://bugs.php.net/fix.php?id=54408&r=support
Expected behavior:                   http://bugs.php.net/fix.php?id=54408&r=notwrong
Not enough info:                     http://bugs.php.net/fix.php?id=54408&r=notenoughinfo
Submitted twice:                     http://bugs.php.net/fix.php?id=54408&r=submittedtwice
register_globals:                    http://bugs.php.net/fix.php?id=54408&r=globals
PHP 4 support discontinued:          http://bugs.php.net/fix.php?id=54408&r=php4
Daylight Savings:                    http://bugs.php.net/fix.php?id=54408&r=dst
IIS Stability:                       http://bugs.php.net/fix.php?id=54408&r=isapi
Install GNU Sed:                     http://bugs.php.net/fix.php?id=54408&r=gnused
Floating point limitations:          http://bugs.php.net/fix.php?id=54408&r=float
No Zend Extensions:                  http://bugs.php.net/fix.php?id=54408&r=nozend
MySQL Configuration Error:           http://bugs.php.net/fix.php?id=54408&r=mysqlcfg



Thread (11 messages)

« previous php.bugs (#159808) next »