#39728 [Bgs]: Bug with global unset of 2 dimensional array

From: Date: Mon, 04 Dec 2006 18:54:29 +0000
Subject: #39728 [Bgs]: Bug with global unset of 2 dimensional array
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-105902@lists.php.net to get a copy of this message
ID: 39728 User updated by: victorepand at hotmail dot com Reported By: victorepand at hotmail dot com Status: Bogus Bug Type: Arrays related Operating System: Unix PHP Version: 5.2.0 New Comment: 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. Previous Comments: ------------------------------------------------------------------------ [2006-12-04 12:10:48] 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 12:04:52] 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. ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=39728&edit=1

« previous php.bugs (#105902) next »