php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #29429 unset( $GLOBALS['foo']); unset only global variable, not variables that REFERED
Submitted: 2004-07-28 15:05 UTC Modified: 2004-07-29 10:04 UTC
From: kingoleg at mail dot ru Assigned:
Status: Not a bug Package: Documentation problem
PHP Version: 4.3.8 OS: All
Private report: No CVE-ID: None
 [2004-07-28 15:05 UTC] kingoleg at mail dot ru
Description:
------------
unset( $GLOBALS['foo']); unset only global variable, not variables that REFERED to global variable

How we can unset global variable with all referenced variable using unset() function? Do we must user assign to null?

Reproduce code:
---------------
<?php
function a( &$foo) {
    $foo = 'ok';
    b( $foo);
    var_dump( $foo);
}

function b( &$foo) {
    $foo = 'not ok';
    unset( $GLOBALS['foo']);
}

global $foo;
$foo = 'global';
a($foo);
var_dump( $foo);

?>

Expected result:
----------------
NULL NULL

Actual result:
--------------
string(6) "not ok" NULL 

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-07-29 09:23 UTC] tony2001@php.net
It's a docu problem, not a bug.
 [2004-07-29 09:44 UTC] kingoleg at mail dot ru
Doc problem about this http://bugs.php.net/?id=29428 was closed with bogus status :)
 [2004-07-29 09:51 UTC] tony2001@php.net
Then it's a bogus docu problem.

 [2004-07-29 10:04 UTC] vrana@php.net
This is slightly different. Explaining just here:

$foo inside a() is reference to value referenced also by $GLOBALS['foo']. $foo inside b() is the same. So there are 3 references to the value. Unseting $GLOBALS['foo'] inside b() destroys only one reference. Returning from b() destroys the second reference. But there still exists one reference to the value ($foo in a()), so var_dump() inside a() prints the value. Last reference is destroyed after returning from a(), so var_dump() outside function prints NULL.

You can understand what's happening if you understand the sentence "$a and $b are completely equal here, that's not $a is pointing to $b or vice versa, that's $a and $b pointing to the same place." written in the manual. So this is really bogus.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Jul 04 16:01:29 2024 UTC