|
|
 |
unset (PHP 3, PHP 4, PHP 5 ) unset -- Unset a given variable Descriptionvoid unset ( mixed var [, mixed var [, mixed ...]] )
unset() destroys the specified variables. Note
that in PHP 3, unset() will always return TRUE
(actually, the integer value 1). In PHP 4, however,
unset() is no longer a true function: it is
now a statement. As such no value is returned, and attempting to
take the value of unset() results in a parse
error.
Example 1. unset() example |
<?php
unset($foo);
unset($bar['quux']);
unset($foo1, $foo2, $foo3);
?>
|
|
The behavior of unset() inside of a function
can vary depending on what type of variable you are attempting to
destroy.
If a globalized variable is unset() inside of
a function, only the local variable is destroyed. The variable
in the calling environment will retain the same value as before
unset() was called.
The above example will output:
If a variable that is PASSED BY REFERENCE is
unset() inside of a function, only the local
variable is destroyed. The variable in the calling environment
will retain the same value as before unset()
was called.
The above example will output:
If a static variable is unset() inside of a
function, unset() destroys the variable and all
its references.
The above example will output:
If you would like to unset() a global variable
inside of a function, you can use
the $GLOBALS array to do so:
Note: Because this is a
language construct and not a function, it cannot be called using
variable functions
See also isset(),
empty(), and
array_splice().
User Contributed Notes
unset
franckraynal at free dot fr
26-Feb-2005 07:02
Here is another way to make 'unset' work with session variables from within a function :
<?php
function unsetSessionVariable ($sessionVariableName) {
unset($GLOBALS[_SESSION][$sessionVariableName]);
}
?>
May it work with others than me...
F.
bedbin at gmail dot com
02-Feb-2005 08:33
usefull tip:
if you have session variables like these.
<?php
echo "<pre>";
$_SESSION["num"] = array(1,2,3,4);
var_dump($_SESSION);
echo "-<br>";
unset($_SESSION);
var_dump($_SESSION);
?>
gives out:
array(1) {
["num"]=>
array(4) {
[0]=>
int(1)
[1]=>
int(2)
[2]=>
int(3)
[3]=>
int(4)
}
}
-
NULL
if you use empty instead unset you get same output as first var_dump($_SESSION) gives.
I hope help sb.
Nghia
05-Jan-2005 10:41
I saw this mentioned somewhere else but if you do
$var = NULL
then I've noticed less memory usuage than with unset(). In fact, unset didn't do anything.
This might be useful if you're doing a php-gtk app, thats starting to consume significant memory over a long period of time. This was the code I used to test
// Check memory before here
for($i = 0; $i < 100; $i++)
{
$dialog = &new GtkDialog();
$dialog->realize();
$dialog->destroy();
$dialog = NULL;
//unset($dialog);
}
// Check memory after here
Doing a difference between after and before results in:
Using destroy() and unset() -> ~31kb
Using $dialog = NULL -> ~13 kb
The expected memory usuage should be 0kb or around there.
harycary at netscape dot net
14-Dec-2004 11:56
If you ever have to unset all the variables of a class from within a funciton of that class use the following code:
<?php
class User
{
function User_login ( ... )
{...}
function User_logout ( $greeting )
{
...
foreach ( array_keys ( get_object_vars ( &$this ) ) as $val)
{ unset( $this->$val ); }
$this->greeting = $greeting;
...
}
}
?>
If anyone knows of a more effective way please post a reply.
mv at brasil dot com
08-Nov-2004 10:04
If you want to remove one element of Query String use this function, than place the returned values in <a href="script.php?'. remove_query("arg1") .'">
function remove_query($key) {
$arrquery = explode("&", $_SERVER["QUERY_STRING"]);
foreach ($arrquery as $query_value) {
$valor = substr($query_value, strpos($query_value, "=") + 1);
$chave = substr($query_value, 0, strpos($query_value, "="));
$querystring[$chave] = $valor;
}
unset($querystring[$key]);
foreach ($querystring as $query_key => $query_value) {
$query[] = "{$query_key}={$query_value}";
}
$query = implode("&", $query);
return $query;
}
dan AT --nospam-- cubeland DOT co DOT uk
04-Nov-2004 01:38
dh at argosign dot de -
it is possible to unset globals from within functions thanks to the $GLOBALS array:
<?php
$x = 10;
function test() {
unset ($GLOBALS['x']);
echo 'x: ' . $GLOBALS['x'] . '<br />';
}
test();
echo "x: $x<br />";
?>
timo dot hummel at 4fb dot de
07-Sep-2004 08:24
For the curious: unset also frees memory of the variable used.
It might be possible that the in-memory size of the PHP Interpreter isn't reduced, but your scripts won't touch the memory_limit boundary. Memory is reused if you declare new variables.
thorry at thorry dot net
05-Aug-2004 04:15
The documentation is not entirely clear when it comes to static variables. It says:
If a static variable is unset() inside of a function, unset() destroys the variable and all its references.
<?php
function foo()
{
static $a;
$a++;
echo "$a\n";
unset($a);
}
foo();
foo();
foo();
?>
The above example would output:
1
2
3
And it does! But the variable is NOT deleted, that's why the value keeps on increasing, otherwise the output would be:
1
1
1
The references are destroyed within the function, this handeling is the same as with global variables, the difference is a static variable is a local variable.
Be carefull using unset and static values as the output may not be what you expect it to be. It appears to be impossible to destroy a static variable. You can only destroy the references within the current executing function, a successive static statement will restore the references.
The documentation would be better if it would say:
"If a static variable is unset() inside of a function, unset() destroys all references to the variable. "
Example: (tested PHP 4.3.7)
<?php
function foo()
{
static $a;
$a++;
echo "$a\n";
unset($a);
echo "$a\n";
static $a;
echo "$a\n";
}
foo();
foo();
foo();
?>
Would output:
1
1
2
2
3
3
anon at no spam dot no address dot com
16-Jul-2004 11:19
Adding on to what bond at noellebond dot com said, if you want to remove an index from the end of the array, if you use unset, the next index value will still be what it would have been.
Eg you have
<?php
$x = array(1, 2);
for ($i = 0; $i < 5; $i++)
{
unset($x[(count($x)-1)]); $x[] = $i;
}
?>
You would expect:
Array([0] => 1, [1] => 4)
as you want it to remove the last set key....
but you actually get
Array ( [0] => 1 [4] => 2 [5] => 3 [6] => 4 )
This is since even though the last key is removed, the auto indexing still keeps its previous value.
The only time where this would not seem right is when you remove a value off the end. I guess different people would want it different ways.
The way around this is to use array_pop() instead of unset() as array_pop() refreshes the autoindexing thing for the array.
<?php
$x = array(1, 2);
for ($i = 0; $i < 5; $i++)
{
array_pop($x); $x[] = $i;
}
?>
This returns the expected value of x = Array([0] => 1, [1] => 4);
Hope this helps someone who may need this for some odd reason, I did.
bond at noellebond dot com
26-May-2004 04:34
Note that though global arrays will not be altered by a function, an array in an object WILL be altered if referenced within one of its methods. For example:
function remove_index ($i)
{
unset($this->test_array[$i]);
$temp_array = array_values($this->test_array);
$this->test_array = $temp_array;
}
Will remove key $i from the object's array and reindex it.
andre at twg dot com dot au
06-Mar-2004 11:16
Only This works with register_globals being 'ON'.
unset( $_SESSION['variable'] );
The above will not work with register_globals turned on (will only work outside of a function).
$variable = $_SESSION['variable'];
unset( $_SESSION['variable'], $variable );
The above will work with register_globals on & inside a function
warhog at warhog dot net
27-Jan-2004 10:52
you may wan't to unset all variables which are defined, here's one way:
<?php
function unset_all_vars($a)
{ foreach($a as $key => $val)
{ unset($GLOBALS[$key]); }
return serialize($a); }
unset_all_vars(get_defined_vars());
?>
you can also save than a serialized var of the "memory" and perhaps store this in a temporary file.. very usefull if you work with text files and/or file uploads when you've got very large variables.
greetz
kdechant at midwestarts dot com
23-Nov-2003 05:47
As of PHP version 4.3.3, unset() results in a parse error if it is used with the @ error suppression operator.
For example:
@unset($var); // parse error
unset(@$var); // parse error
unset($var); // okay
frank at agentbrand dot com
09-Nov-2003 11:59
Use array_values() after unset() to reindex your array.
Note that unset() removes the index as a key, you will need to reindex your array again to get expected behavior
vmizuba at queens dot org
27-Oct-2003 09:25
for what it's worth...
in php 4.1, using unset to destroy a session variable, i.e. unset($_SESSION['variable']); destroys it by erasing variable information but leaves behind the variable name appended with a '!' in front of the name in the session file... leaving the session file larger and x bytes wasted depending on the variable name length
| |