Sophie

Sophie

distrib > Mageia > 7 > i586 > by-pkgid > 2b917e0437961edec048f1d15e2d7449 > files > 6995

php-manual-en-7.2.11-1.mga7.noarch.rpm

<!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, PHP 7)</p><p class="refpurpose"><span class="refname">unset</span> &mdash; <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">&lt;?php<br /></span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">destroy_foo</span><span style="color: #007700">()&nbsp;<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;global&nbsp;</span><span style="color: #0000BB">$foo</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;unset(</span><span style="color: #0000BB">$foo</span><span style="color: #007700">);<br />}<br /><br /></span><span style="color: #0000BB">$foo&nbsp;</span><span style="color: #007700">=&nbsp;</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&nbsp;</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">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">&lt;?php<br /></span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">foo</span><span style="color: #007700">()&nbsp;<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;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&nbsp;</span><span style="color: #007700">=&nbsp;</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="informalexample">
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">foo</span><span style="color: #007700">(&amp;</span><span style="color: #0000BB">$bar</span><span style="color: #007700">)&nbsp;<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;unset(</span><span style="color: #0000BB">$bar</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$bar&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"blah"</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">$bar&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'something'</span><span style="color: #007700">;<br />echo&nbsp;</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&nbsp;</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">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">&lt;?php<br /></span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">foo</span><span style="color: #007700">()<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;static&nbsp;</span><span style="color: #0000BB">$bar</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$bar</span><span style="color: #007700">++;<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"Before&nbsp;unset:&nbsp;</span><span style="color: #0000BB">$bar</span><span style="color: #DD0000">,&nbsp;"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;unset(</span><span style="color: #0000BB">$bar</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$bar&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">23</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"after&nbsp;unset:&nbsp;</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>
   <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>
<code class="parameter">var</code></dt>

     <dd>

      <p class="para">
       The variable to be unset.
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">...</code></dt>

     <dd>

      <p class="para">
       Another variable ...
      </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-6519">
    <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">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;destroy&nbsp;a&nbsp;single&nbsp;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">//&nbsp;destroy&nbsp;a&nbsp;single&nbsp;element&nbsp;of&nbsp;an&nbsp;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">//&nbsp;destroy&nbsp;more&nbsp;than&nbsp;one&nbsp;variable<br /></span><span style="color: #007700">unset(</span><span style="color: #0000BB">$foo1</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$foo2</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$foo3</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
   <div class="example" id="example-6520">
    <p><strong>Example #2 Using <em>(unset)</em> casting</strong></p>
    <div class="example-contents"><p>
     <a href="language.types.null.html#language.types.null.casting" class="link"><em>(unset)</em></a>
     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&#039;s casting.
     The (unset) cast is deprecated as of PHP 7.2.0.
    </p></div>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$name&nbsp;</span><span style="color: #007700">=&nbsp;</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)&nbsp;</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">?&gt;</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) &quot;Felipe&quot;
</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>
    <li class="member"><a href="language.types.null.html#language.types.null.casting" class="link">(unset) casting</a></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>