<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/ref.var.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'en',
  ),
  'this' => 
  array (
    0 => 'function.unset.php',
    1 => 'unset',
    2 => 'unset a given variable',
  ),
  'up' => 
  array (
    0 => 'ref.var.php',
    1 => 'Variable handling Functions',
  ),
  'prev' => 
  array (
    0 => 'function.unserialize.php',
    1 => 'unserialize',
  ),
  'next' => 
  array (
    0 => 'function.var-dump.php',
    1 => 'var_dump',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'reference/var/functions/unset.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="function.unset" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">unset</h1>
  <p class="verinfo">(PHP 4, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">unset</span> &mdash; <span class="dc-title"><span class="function"><strong>unset()</strong></span> 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="methodname"><strong>unset</strong></span>(<span class="methodparam"><span class="type"><a href="language.types.mixed.php" class="type mixed">mixed</a></span> <code class="parameter">$var</code></span>, <span class="methodparam"><span class="type"><a href="language.types.mixed.php" class="type mixed">mixed</a></span> <code class="parameter">...$vars</code></span>): <span class="type"><a href="language.types.void.php" class="type void">void</a></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="example" id="example-1">
    <p><strong>Example #1 Using <span class="function"><strong>unset()</strong></span></strong></p>
    <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?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">?&gt;</span></span></code></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"><a href="reserved.variables.globals.php" class="classname">$GLOBALS</a></var> array to do so:
   <div class="example" id="example-2">
    <p><strong>Example #2 <span class="function"><strong>unset()</strong></span> a Global Variable</strong></p>
    <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?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">?&gt;</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="example" id="example-3">
    <p><strong>Example #3 <span class="function"><strong>unset()</strong></span> with Reference</strong></p>
    <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">function </span><span style="color: #0000BB">foo</span><span style="color: #007700">(&amp;</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">?&gt;</span></span></code></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="example" id="example-4">
    <p><strong>Example #4 <span class="function"><strong>unset()</strong></span> with Static Variable</strong></p>
    <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?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">?&gt;</span></span></code></div>
    </div>

   </div>
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.unset-parameters">
  <h3 class="title">Parameters</h3>
  <p class="para">
   <dl>
    
     <dt><code class="parameter">var</code></dt>
     <dd>
      <p class="para">
       The variable to be unset.
      </p>
     </dd>
    
    
     <dt><code class="parameter">vars</code></dt>
     <dd>
      <p class="para">
       Further variables.
      </p>
     </dd>
    
   </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 examples" id="refsect1-function.unset-examples">
  <h3 class="title">Examples</h3>
  <p class="para">
   <div class="example" id="example-5">
    <p><strong>Example #5 <span class="function"><strong>unset()</strong></span> example</strong></p>
    <div class="example-contents">
<div class="annotation-non-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?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">?&gt;</span></span></code></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.php" class="link">variable functions</a>,
or <a href="functions.arguments.php#functions.named-arguments" class="link">named arguments</a>.</span>
</p></blockquote>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <p class="para">
    It is possible to unset object properties visible in the current context.
   </p>
   <p class="para">
     If declared,
     <a href="language.oop5.overloading.php#object.get" class="link">__get()</a>
     is called when accessing an unset property, and
     <a href="language.oop5.overloading.php#object.set" class="link">__set()</a>
     is called when setting an unset property.
   </p>
  </p></blockquote>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <p class="para">
    It is not possible to unset <code class="literal">$this</code> inside an object
    method.
   </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.php#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><span class="function"><a href="function.isset.php" class="function" rel="rdfs-seeAlso">isset()</a> - Determine if a variable is declared and is different than null</span></li>
    <li><span class="function"><a href="function.empty.php" class="function" rel="rdfs-seeAlso">empty()</a> - Determine whether a variable is empty</span></li>
    <li><a href="language.oop5.overloading.php#object.unset" class="link">__unset()</a></li>
    <li><span class="function"><a href="function.array-splice.php" class="function" rel="rdfs-seeAlso">array_splice()</a> - Remove a portion of the array and replace it with something else</span></li>
    <li><a href="language.types.null.php#language.types.null.casting" class="link">(unset) casting</a></li>
   </ul>
  </p>
 </div>


</div><?php manual_footer($setup); ?>