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

 ID:                 63865
 Updated by:         krak...@php.net
 Reported by:        petermiller1986 at gmail dot com
 Summary:            php unset local reference affecting global scope
-Status:             Open
+Status:             Closed
 Type:               Bug
 Package:            *General Issues
 Operating System:   ubuntu 10.04
 PHP Version:        5.3.20
-Assigned To:        
+Assigned To:        krakjoe
 Block user comment: N
 Private report:     N

 New Comment:

You set the data in the global scope with the return of the function.
Your unset function does not receive the array by reference.
See the code below, commenting out the reference makes no difference.

<?php
function should_not_alter($in)
{
    $in_ref = &$in['level1']; //try commenting out this line to see the output 
change

    should_only_unset_locally($in);

    return $in;
}

function should_only_unset_locally(&$in)
{
    unset($in['level1']['level2_0']);
}

$data[0] = array(
    'level1' => array(
        'level2_0' => 'first value',
        'level2_1' => 'second value'
    )
);

$data[1]=should_not_alter($data[0]);

print_r($data);
?>


Previous Comments:
------------------------------------------------------------------------
[2012-12-28 04:53:56] petermiller1986 at gmail dot com

Description:
------------
i have come across some very strange php behaviour. an unset which should occur 
within local scope is affecting the scope of the caller function. if you run 
the test script you will see that the value 'first value' has been unset from 
the $data array in the global scope. however if you comment out the $in_ref =& 
$in['level1']; line then the result changes (and it shouldn't i think).

is this intended behaviour of php or a bug?


Test script:
---------------
<?php
function should_not_alter($in)
{
    $in_ref =& $in['level1']; //try commenting out this line to see the output 
change
    should_only_unset_locally($in);
    return $in;
}
function should_only_unset_locally($in)
{
    unset($in['level1']['level2_0']);
}
$data = array('level1' => array('level2_0' => 'first value', 'level2_1' => 
'second value'));
$data = should_not_alter($data);
print_r($data);
?>

Expected result:
----------------
Array
(
    [level1] => Array
        (
            [level2_0] => first value
            [level2_1] => second value
        )

)

Actual result:
--------------
Array
(
    [level1] => Array
        (
            [level2_1] => second value
        )

)



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



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

Reply via email to