php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #39728 Bug with global unset of 2 dimensional array
Submitted: 2006-12-04 12:04 UTC Modified: 2006-12-04 18:54 UTC
From: victorepand at hotmail dot com Assigned:
Status: Not a bug Package: Arrays related
PHP Version: 5.2.0 OS: Unix
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: victorepand at hotmail dot com
New email:
PHP Version: OS:

 

 [2006-12-04 12:04 UTC] victorepand at hotmail dot com
Description:
------------
When trying to unset a 2 dimensional array from within a function like this (PHP 5.1):
   unset($GLOBALS["products"]);
elements like $products[123]["id"] continue to exist!

I had to use this command to unset the entire array, which is very inefficient:
   foreach($products AS $their_product_id=>$ar) unset($GLOBALS["products"][$their_product_id]);


Reproduce code:
---------------
unset($GLOBALS["products"]);

Expected result:
----------------
elements like $products[123]["id"] should no longer exist.

Actual result:
--------------
Elements like $products[123]["id"] continue to exist.

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2006-12-04 12:10 UTC] tony2001@php.net
"$GLOBALS - Contains a reference to every variable which is currently available within the global scope of the script. The keys of this array are the names of the global variables." (c) http://www.php.net/manual/en/language.variables.predefined.php

With unset($GLOBALS['varname']); you unset the reference, not the variable itself, which is equal to:
<?php
$var = "string";
$ref =& $var;
unset($ref); //unset the reference
var_dump($var); //but the $var is still here
?>
 [2006-12-04 18:54 UTC] victorepand at hotmail dot com
You are wrong. unset($GLOBALS["variable"]) is the recommended way to remove global variables from within a function, it does NOT remove local references. To prove my point, the PHP manual states this here:

http://www.php.net/distributions/manual/php_manual_en.html.gz#function.unset

If you would like to unset() a global variable inside of a function, you can use the $GLOBALS array to do so: 

<?php
function foo() 
{
    unset($GLOBALS['bar']);
}

$bar = "something";
foo();
?>
 
_______________________________________

So that is exactly what I was doing, but I was trying to unset a 2 dimensional array rather than a simple variable, and it does not work like it should.

The PHP manual also states:

Handling of global variables
While handling of global variables had the focus on to be easy in PHP 3 and early versions of PHP 4, the focus has changed to be more secure. While in PHP 3 the following example worked fine, in PHP 4 it has to be unset(unset($GLOBALS["id"]));. This is only one issue of global variable handling. You should always have used $GLOBALS, with newer versions of PHP 4 you are forced to do so in most cases. Read more on this subject in the global references section.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Sep 28 02:01:27 2024 UTC