From:             
Operating system: CentOS 5.5
PHP version:      5.3.5
Package:          Variables related
Bug Type:         Bug
Bug description:scalar reference of array element has unintended side effects

Description:
------------
Adding a scalar reference to an array element changes array assignment
behavior.   

In a regular array assignment, elements are all copied by value.  But if a
scalar 

reference has been made to an array element, that element is copied by
reference 

in a subsequent array assignment.  The code looks exactly the same, so the


behavior shouldn't change just because there's a reference floating out
there 

somewhere.

Test script:
---------------
<?php

$arr1 = array(1);

echo "\nbefore:\n";

echo "\$arr1[0] == {$arr1[0]}\n";

$arr2 = $arr1;

$arr2[0]++;

echo "\nafter:\n";

echo "\$arr1[0] == {$arr1[0]}\n";

echo "\$arr2[0] == {$arr2[0]}\n";

$arr3 = array(1);

$a =& $arr3[0];

echo "\nbefore:\n";

echo "\$a == $a\n";

echo "\$arr3[0] == {$arr3[0]}\n";

$arr4 = $arr3;

$arr4[0]++;

echo "\nafter:\n";

echo "\$a == $a\n";

echo "\$arr3[0] == {$arr3[0]}\n";

echo "\$arr4[0] == {$arr4[0]}\n";

Expected result:
----------------
before:

$arr1[0] == 1



after:

$arr1[0] == 1

$arr2[0] == 2



before:

$a == 1

$arr3[0] == 1



after:

$a == 1

$arr3[0] == 1

$arr4[0] == 2



Actual result:
--------------
before:

$arr1[0] == 1



after:

$arr1[0] == 1

$arr2[0] == 2



before:

$a == 1

$arr3[0] == 1



after:

$a == 2

$arr3[0] == 2

$arr4[0] == 2



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

Reply via email to