★ wanayoo — archive 1999 http://www.php.net/manual/function.unset.phpNouvelle recherche | Portail wanayoo

PHP Home Page

Manual Table of Contents
Up to Variables
Quick Reference
German version of this pageJapanese version of this pageItalian version of this pageFrench version of this pageHungarian version of this page
Variables
* call_user_func
* doubleval
* empty
* gettype
* intval
* is_array
* is_bool
* is_double
* is_float
* is_int
* is_integer
* is_long
* is_numeric
* is_object
* is_real
* is_resource
* is_string
* isset
* print_r
* settype
* strval
* unset
* var_dump
Manual: unset
View the source code for this pageSearch the site



Previous page
 strval
 Updated
Sat, 12 Aug 2000
var_dump 
Next page


unset

(unknown)

unset -- Unset a given variable

Description

int unset (mixed var)

unset() destroys the specified variable and returns true.

Example 1. Unset() example


unset ($foo);
unset ($bar['quux']);
      

See also isset() and empty().


User Contributed Notes: unset


sander@pilon.com
12-Aug-1999 03:13
PHP4 incompatibility: When used on static variables in funnctions, they lose their static property. (So, dont use them on static variables.)

This behaviour is different then PHP3 behaviour, and it is one of the incompatibilities. According to Zeev, PHP4 displays correct behaviour and PHP3 behaviour of unset() can be called "not very well defined."

See bug 2015 for an example.



robin@games.lostboys.nl
23-Mar-2000 08:43
Is there a way to unset() a function?


aschneid@mail.slc.edu
16-Apr-2000 03:44
Also, is there a way to undefine a constant?


tjw@tjw.org
28-Apr-2000 12:22
More on PHP 4 compatability:

unset() in PHP4 removes the symbol from the symbol table.

This can be confusing when using PASS BY REFERENCE, global, or static variables. For instance, if you pass a variable by reference to a function like this:
<pre>
function foo(&$bar) {
unset($bar);
$bar = "blah";
}
</pre>

$bar will NOT be set in the calling environment, like in would be in PHP3 because the symbol $bar was removed from the calling environment's symobol table with unset(). Therefore, when setting $bar to "blah", it will only be set in the environment of the function foo().




yassine@cybercable.fr
05-May-2000 05:18
Does unset decrease the index of an array if you unset an element of this array ??


bbarringer@infistar.com
24-May-2000 05:30
With PHP4.0.0 Unset on a Global variable requires redeclaring the Global variable after call to unset.
<PRE>
global $myarray;
unset($myarray);
global $myarray;
</PRE>



kingyip@bigfoot.com
12-Jul-2000 02:16
Does unset release memory for that variable?


dream@aevum.net
14-Jul-2000 03:05
This function does reduce the index of an array, so if $joy has 5 elements and you do unset($joy[2]), the array will have only 4 elements.

This means you should be careful when using <pre>for</pre> loops and the unset() function, like below:

<pre>for ($i=0; $i<count($joy); $i++) {
if ($joy[$i] != "fun") {
unset($joy[i]);
}
}</pre>

If the element isn't "fun", the count of $joy will be reduced by one, making your loop run fewer times than it should. A remedy would be to make a variable equal to the count ahead of time, like <pre>$joylen = count($joy);</pre>, then use that in the for loop instead.



dream@aevum.net
07-Aug-2000 04:38
Also, for some stupid, stupid reason, when you use unset() on an array, it not only removes the item but it reduces the index, and not in a way you'd expect.

For example:

$dope = array("a", "b", "c", "d", "e");
unset($dope[1]);

What *should* happen is the second array item (b) should disappear. What *does* happen is that $dope[1] is unset, AND the index is reduced, but the item isn't deleted. So your array now goes "a", (blank), "c", "d". Both "b" and "e" were lost.

Very annoying.



 About Notes


Previous page
 strval
 Updated
Sat, 12 Aug 2000
var_dump 
Next page





Who's responsible for this?
Top of this page

Site
Hosting:



Located in
United States
Elements of this website are subject to copyright.
Questions about installing or using PHP should be directed to one of the mailing lists.
Only questions about the website should be directed to webmaster@php.net.