<HTML ><HEAD ><TITLE >unset</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK REL="HOME" TITLE="PHP Manual" HREF="index.html"><LINK REL="UP" TITLE="Variable Functions" HREF="ref.variables.html"><LINK REL="PREVIOUS" TITLE="unserialize" HREF="function.unserialize.html"><LINK REL="NEXT" TITLE="var_dump" HREF="function.var-dump.html"><META HTTP-EQUIV="Content-type" CONTENT="text/html; charset=ISO-8859-1"></HEAD ><BODY CLASS="refentry" BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#840084" ALINK="#0000FF" ><DIV CLASS="NAVHEADER" ><TABLE SUMMARY="Header navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TH COLSPAN="3" ALIGN="center" >PHP Manual</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="function.unserialize.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" ></TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="function.var-dump.html" ACCESSKEY="N" >Next</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><H1 ><A NAME="function.unset" ></A >unset</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN94516" ></A ><P > (PHP 3, PHP 4 )</P >unset -- Unset a given variable</DIV ><DIV CLASS="refsect1" ><A NAME="AEN94519" ></A ><H2 >Description</H2 >void <B CLASS="methodname" >unset</B > ( mixed var [, mixed var [, ...]])<BR ></BR ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Note: </B > <B CLASS="function" >unset()</B > is a language construct. </P ></BLOCKQUOTE ></DIV ><P > <B CLASS="function" >unset()</B > destroys the specified variables. Note that in PHP 3, <B CLASS="function" >unset()</B > will always return <TT CLASS="constant" ><B >TRUE</B ></TT > (actually, the integer value 1). In PHP 4, however, <B CLASS="function" >unset()</B > is no longer a true function: it is now a statement. As such no value is returned, and attempting to take the value of <B CLASS="function" >unset()</B > results in a parse error. </P ><P > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN94542" ></A ><P ><B >Example 1. <B CLASS="function" >unset()</B > example</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" >// destroy a single variable unset ($foo); // destroy a single element of an array unset ($bar['quux']); // destroy more than one variable unset ($foo1, $foo2, $foo3);</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P > The behavior of <B CLASS="function" >unset()</B > inside of a function can vary depending on what type of variable you are attempting to destroy. </P ><P > If a globalized variable is <B CLASS="function" >unset()</B > inside of a function, only the local variable is destroyed. The variable in the calling environment will retain the same value as before <B CLASS="function" >unset()</B > was called. <DIV CLASS="informalexample" ><A NAME="AEN94551" ></A ><P ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" >function destroy_foo() { global $foo; unset($foo); } $foo = 'bar'; destroy_foo(); echo $foo;</PRE ></TD ></TR ></TABLE ><P ></P ></DIV > The above example would output: <DIV CLASS="informalexample" ><A NAME="AEN94553" ></A ><P ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="screen" >bar</PRE ></TD ></TR ></TABLE ><P ></P ></DIV > </P ><P > If a variable that is PASSED BY REFERENCE is <B CLASS="function" >unset()</B > inside of a function, only the local variable is destroyed. The variable in the calling environment will retain the same value as before <B CLASS="function" >unset()</B > was called. <DIV CLASS="informalexample" ><A NAME="AEN94558" ></A ><P ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" >function foo(&$bar) { unset($bar); $bar = "blah"; } $bar = 'something'; echo "$bar\n"; foo($bar); echo "$bar\n";</PRE ></TD ></TR ></TABLE ><P ></P ></DIV > The above example would output: <DIV CLASS="informalexample" ><A NAME="AEN94560" ></A ><P ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="screen" >something something</PRE ></TD ></TR ></TABLE ><P ></P ></DIV > </P ><P > If a static variable is <B CLASS="function" >unset()</B > inside of a function, <B CLASS="function" >unset()</B > destroyes the variable and all its references. <DIV CLASS="informalexample" ><A NAME="AEN94565" ></A ><P ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" >function foo() { static $a; $a++; echo "$a\n"; unset($a); } foo(); foo(); foo();</PRE ></TD ></TR ></TABLE ><P ></P ></DIV > The above example would output: <DIV CLASS="informalexample" ><A NAME="AEN94567" ></A ><P ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="screen" >1 2 3</PRE ></TD ></TR ></TABLE ><P ></P ></DIV > </P ><P > If you would like to <B CLASS="function" >unset()</B > a global variable inside of a function, you can use the <TT CLASS="varname" >$GLOBALS</TT > array to do so: <DIV CLASS="informalexample" ><A NAME="AEN94572" ></A ><P ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" >function foo() { unset($GLOBALS['bar']); } $bar = "something"; foo();</PRE ></TD ></TR ></TABLE ><P ></P ></DIV > </P ><P > See also <A HREF="function.isset.html" ><B CLASS="function" >isset()</B ></A > and <A HREF="function.empty.html" ><B CLASS="function" >empty()</B ></A >. </P ></DIV ><DIV CLASS="NAVFOOTER" ><HR ALIGN="LEFT" WIDTH="100%"><TABLE SUMMARY="Footer navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><A HREF="function.unserialize.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >Home</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="function.var-dump.html" ACCESSKEY="N" >Next</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >unserialize</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="ref.variables.html" ACCESSKEY="U" >Up</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >var_dump</TD ></TR ></TABLE ></DIV ></BODY ></HTML >