<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Unset a given variable</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.unserialize.html">unserialize</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.var-dump.html">var_dump</a></div> <div class="up"><a href="ref.var.html">Variable handling Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="function.unset" class="refentry"> <div class="refnamediv"> <h1 class="refname">unset</h1> <p class="verinfo">(PHP 4, PHP 5)</p><p class="refpurpose"><span class="refname">unset</span> — <span class="dc-title">Unset a given variable</span></p> </div> <div class="refsect1 description" id="refsect1-function.unset-description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="type"><span class="type void">void</span></span> <span class="methodname"><strong>unset</strong></span> ( <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <code class="parameter">$var</code></span> [, <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <code class="parameter">$...</code></span> ] )</div> <p class="para rdfs-comment"> <span class="function"><strong>unset()</strong></span> destroys the specified variables. </p> <p class="para"> The behavior of <span class="function"><strong>unset()</strong></span> inside of a function can vary depending on what type of variable you are attempting to destroy. </p> <p class="para"> If a globalized variable is <span class="function"><strong>unset()</strong></span> inside of a function, only the local variable is destroyed. The variable in the calling environment will retain the same value as before <span class="function"><strong>unset()</strong></span> was called. <div class="informalexample"> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /></span><span style="color: #007700">function </span><span style="color: #0000BB">destroy_foo</span><span style="color: #007700">() <br />{<br /> global </span><span style="color: #0000BB">$foo</span><span style="color: #007700">;<br /> unset(</span><span style="color: #0000BB">$foo</span><span style="color: #007700">);<br />}<br /><br /></span><span style="color: #0000BB">$foo </span><span style="color: #007700">= </span><span style="color: #DD0000">'bar'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">destroy_foo</span><span style="color: #007700">();<br />echo </span><span style="color: #0000BB">$foo</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </p> <p class="para">The above example will output:</p> <p class="para"> <div class="informalexample"> <div class="example-contents screen"> <div class="cdata"><pre> bar </pre></div> </div> </div> </p> <p class="para"> To <span class="function"><strong>unset()</strong></span> a global variable inside of a function, then use the <var class="varname"><var class="varname"><a href="reserved.variables.globals.html" class="classname">$GLOBALS</a></var></var> array to do so: <div class="informalexample"> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /></span><span style="color: #007700">function </span><span style="color: #0000BB">foo</span><span style="color: #007700">() <br />{<br /> unset(</span><span style="color: #0000BB">$GLOBALS</span><span style="color: #007700">[</span><span style="color: #DD0000">'bar'</span><span style="color: #007700">]);<br />}<br /><br /></span><span style="color: #0000BB">$bar </span><span style="color: #007700">= </span><span style="color: #DD0000">"something"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">foo</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </p> <p class="para"> If a variable that is PASSED BY REFERENCE is <span class="function"><strong>unset()</strong></span> inside of a function, only the local variable is destroyed. The variable in the calling environment will retain the same value as before <span class="function"><strong>unset()</strong></span> was called. <div class="informalexample"> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /></span><span style="color: #007700">function </span><span style="color: #0000BB">foo</span><span style="color: #007700">(&</span><span style="color: #0000BB">$bar</span><span style="color: #007700">) <br />{<br /> unset(</span><span style="color: #0000BB">$bar</span><span style="color: #007700">);<br /> </span><span style="color: #0000BB">$bar </span><span style="color: #007700">= </span><span style="color: #DD0000">"blah"</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">$bar </span><span style="color: #007700">= </span><span style="color: #DD0000">'something'</span><span style="color: #007700">;<br />echo </span><span style="color: #DD0000">"</span><span style="color: #0000BB">$bar</span><span style="color: #DD0000">\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">foo</span><span style="color: #007700">(</span><span style="color: #0000BB">$bar</span><span style="color: #007700">);<br />echo </span><span style="color: #DD0000">"</span><span style="color: #0000BB">$bar</span><span style="color: #DD0000">\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </p> <p class="para">The above example will output:</p> <p class="para"> <div class="informalexample"> <div class="example-contents screen"> <div class="cdata"><pre> something something </pre></div> </div> </div> </p> <p class="para"> If a static variable is <span class="function"><strong>unset()</strong></span> inside of a function, <span class="function"><strong>unset()</strong></span> destroys the variable only in the context of the rest of a function. Following calls will restore the previous value of a variable. <div class="informalexample"> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /></span><span style="color: #007700">function </span><span style="color: #0000BB">foo</span><span style="color: #007700">()<br />{<br /> static </span><span style="color: #0000BB">$bar</span><span style="color: #007700">;<br /> </span><span style="color: #0000BB">$bar</span><span style="color: #007700">++;<br /> echo </span><span style="color: #DD0000">"Before unset: </span><span style="color: #0000BB">$bar</span><span style="color: #DD0000">, "</span><span style="color: #007700">;<br /> unset(</span><span style="color: #0000BB">$bar</span><span style="color: #007700">);<br /> </span><span style="color: #0000BB">$bar </span><span style="color: #007700">= </span><span style="color: #0000BB">23</span><span style="color: #007700">;<br /> echo </span><span style="color: #DD0000">"after unset: </span><span style="color: #0000BB">$bar</span><span style="color: #DD0000">\n"</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">foo</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">foo</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">foo</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </p> <p class="para">The above example will output:</p> <p class="para"> <div class="informalexample"> <div class="example-contents screen"> <div class="cdata"><pre> Before unset: 1, after unset: 23 Before unset: 2, after unset: 23 Before unset: 3, after unset: 23 </pre></div> </div> </div> </p> </div> <div class="refsect1 parameters" id="refsect1-function.unset-parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <span class="term"><em><code class="parameter">var</code></em></span> <dd> <p class="para"> The variable to be unset. </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">...</code></em></span> <dd> <p class="para"> Another variable ... </p> </dd> </dt> </dl> </p> </div> <div class="refsect1 returnvalues" id="refsect1-function.unset-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> No value is returned. </p> </div> <div class="refsect1 changelog" id="refsect1-function.unset-changelog"> <h3 class="title">Changelog</h3> <p class="para"> <table class="doctable informaltable"> <thead> <tr> <th>Version</th> <th>Description</th> </tr> </thead> <tbody class="tbody"> <tr> <td>4.0.1</td> <td> Added support for multiple arguments. </td> </tr> </tbody> </table> </p> </div> <div class="refsect1 examples" id="refsect1-function.unset-examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example" id="example-5276"> <p><strong>Example #1 <span class="function"><strong>unset()</strong></span> example</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /></span><span style="color: #FF8000">// destroy a single variable<br /></span><span style="color: #007700">unset(</span><span style="color: #0000BB">$foo</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// destroy a single element of an array<br /></span><span style="color: #007700">unset(</span><span style="color: #0000BB">$bar</span><span style="color: #007700">[</span><span style="color: #DD0000">'quux'</span><span style="color: #007700">]);<br /><br /></span><span style="color: #FF8000">// destroy more than one variable<br /></span><span style="color: #007700">unset(</span><span style="color: #0000BB">$foo1</span><span style="color: #007700">, </span><span style="color: #0000BB">$foo2</span><span style="color: #007700">, </span><span style="color: #0000BB">$foo3</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> <div class="example" id="example-5277"> <p><strong>Example #2 Using <em>(unset)</em> casting</strong></p> <div class="example-contents"><p> <em>(unset)</em> casting is often confused with the <span class="function"><strong>unset()</strong></span> function. <em>(unset)</em> casting serves only as a <em>NULL</em>-type cast, for completeness. It does not alter the variable it's casting. </p></div> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />$name </span><span style="color: #007700">= </span><span style="color: #DD0000">'Felipe'</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">((unset) </span><span style="color: #0000BB">$name</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$name</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> <div class="example-contents"><p>The above example will output:</p></div> <div class="example-contents screen"> <div class="cdata"><pre> NULL string(6) "Felipe" </pre></div> </div> </div> </p> </div> <div class="refsect1 notes" id="refsect1-function.unset-notes"> <h3 class="title">Notes</h3> <blockquote class="note"><p><strong class="note">Note</strong>: <span class="simpara">Because this is a language construct and not a function, it cannot be called using <a href="functions.variable-functions.html" class="link">variable functions</a>.</span> </p></blockquote> <blockquote class="note"><p><strong class="note">Note</strong>: <p class="para"> It is possible to unset even object properties visible in current context. </p> </p></blockquote> <blockquote class="note"><p><strong class="note">Note</strong>: <p class="para"> It is not possible to unset <em>$this</em> inside an object method since PHP 5. </p> </p></blockquote> <blockquote class="note"><p><strong class="note">Note</strong>: <p class="para"> When using <span class="function"><strong>unset()</strong></span> on inaccessible object properties, the <a href="language.oop5.overloading.html#object.unset" class="link">__unset()</a> overloading method will be called, if declared. </p> </p></blockquote> </div> <div class="refsect1 seealso" id="refsect1-function.unset-seealso"> <h3 class="title">See Also</h3> <p class="para"> <ul class="simplelist"> <li class="member"> <span class="function"><a href="function.isset.html" class="function" rel="rdfs-seeAlso">isset()</a> - Determine if a variable is set and is not NULL</span></li> <li class="member"> <span class="function"><a href="function.empty.html" class="function" rel="rdfs-seeAlso">empty()</a> - Determine whether a variable is empty</span></li> <li class="member"><a href="language.oop5.overloading.html#object.unset" class="link">__unset()</a></li> <li class="member"> <span class="function"><a href="function.array-splice.html" class="function" rel="rdfs-seeAlso">array_splice()</a> - Remove a portion of the array and replace it with something else</span></li> </ul> </p> </div> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.unserialize.html">unserialize</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.var-dump.html">var_dump</a></div> <div class="up"><a href="ref.var.html">Variable handling Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>