Unset a given variable (original) (raw)
(PHP 4, PHP 5, PHP 7)
unset — Unset a given variable
Description
unset ( mixed $var
[, mixed $...
] ) : void
The behavior of unset() inside of a function can vary depending on what type of variable you are attempting to destroy.
If a globalized variable is unset() inside of a function, only the local variable is destroyed. The variable in the calling environment will retain the same value as beforeunset() was called.
<?php function destroy_foo() { global $foo; unset($foo); }$foo = 'bar'; destroy_foo(); echo $foo; ?>
The above example will output:
To unset() a global variable inside of a function, then use the $GLOBALS array to do so:
<?php function foo() { unset($GLOBALS['bar']); }$bar = "something"; foo(); ?>
If a variable that is PASSED BY REFERENCE isunset() inside of a function, only the local variable is destroyed. The variable in the calling environment will retain the same value as before unset() was called.
<?php function foo(&$bar) { unset($bar); $bar = "blah"; }$bar = 'something'; echo "$bar\n";foo($bar); echo "$bar\n"; ?>
The above example will output:
If a static variable is unset() inside of a function, unset() destroys the variable only in the context of the rest of a function. Following calls will restore the previous value of a variable.
<?php function foo() { static $bar; $bar++; echo "Before unset: $bar, "; unset($bar); $bar = 23; echo "after unset: $bar\n"; }foo(); foo(); foo(); ?>
The above example will output:
Before unset: 1, after unset: 23 Before unset: 2, after unset: 23 Before unset: 3, after unset: 23
Parameters
var
The variable to be unset.
...
Another variable ...
Return Values
No value is returned.
Examples
Example #1 unset() example
<?php // destroy a single variable unset($foo);// destroy a single element of an array unset($bar['quux']);// destroy more than one variable unset($foo1, <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>f</mi><mi>o</mi><mi>o</mi><mn>2</mn><mo separator="true">,</mo></mrow><annotation encoding="application/x-tex">foo2, </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8889em;vertical-align:-0.1944em;"></span><span class="mord mathnormal" style="margin-right:0.10764em;">f</span><span class="mord mathnormal">oo</span><span class="mord">2</span><span class="mpunct">,</span></span></span></span>foo3); ?>
Example #2 Using (unset)
casting
(unset) casting is often confused with theunset() function. (unset)
casting serves only as a NULL
-type cast, for completeness. It does not alter the variable it's casting. The (unset) cast is deprecated as of PHP 7.2.0.
<?php <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi><msup><mo>=</mo><mo mathvariant="normal" lspace="0em" rspace="0em">′</mo></msup><mi>F</mi><mi>e</mi><mi>l</mi><mi>i</mi><mi>p</mi><msup><mi>e</mi><mo mathvariant="normal" lspace="0em" rspace="0em">′</mo></msup><mo separator="true">;</mo><mi>v</mi><mi>a</mi><msub><mi>r</mi><mi>d</mi></msub><mi>u</mi><mi>m</mi><mi>p</mi><mo stretchy="false">(</mo><mo stretchy="false">(</mo><mi>u</mi><mi>n</mi><mi>s</mi><mi>e</mi><mi>t</mi><mo stretchy="false">)</mo></mrow><annotation encoding="application/x-tex">name = 'Felipe';var_dump((unset) </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.7519em;"></span><span class="mord mathnormal">nam</span><span class="mord mathnormal">e</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel"><span class="mrel">=</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.7519em;"><span style="top:-3.063em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">′</span></span></span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:1.0019em;vertical-align:-0.25em;"></span><span class="mord mathnormal" style="margin-right:0.13889em;">F</span><span class="mord mathnormal">e</span><span class="mord mathnormal" style="margin-right:0.01968em;">l</span><span class="mord mathnormal">i</span><span class="mord mathnormal">p</span><span class="mord"><span class="mord mathnormal">e</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.7519em;"><span style="top:-3.063em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">′</span></span></span></span></span></span></span></span></span><span class="mpunct">;</span><span class="mspace" style="margin-right:0.1667em;"></span><span class="mord mathnormal" style="margin-right:0.03588em;">v</span><span class="mord mathnormal">a</span><span class="mord"><span class="mord mathnormal" style="margin-right:0.02778em;">r</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.3361em;"><span style="top:-2.55em;margin-left:-0.0278em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mathnormal mtight">d</span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mord mathnormal">u</span><span class="mord mathnormal">m</span><span class="mord mathnormal">p</span><span class="mopen">((</span><span class="mord mathnormal">u</span><span class="mord mathnormal">n</span><span class="mord mathnormal">se</span><span class="mord mathnormal">t</span><span class="mclose">)</span></span></span></span>name); var_dump($name); ?>
The above example will output:
Notes
Note: Because this is a language construct and not a function, it cannot be called usingvariable functions.
Note:
It is possible to unset even object properties visible in current context.
Note:
It is not possible to unset
$this
inside an object method.
Note:
When using unset() on inaccessible object properties, the __unset() overloading method will be called, if declared.
See Also
- isset() - Determine if a variable is declared and is different than NULL
- empty() - Determine whether a variable is empty
- __unset()
- array_splice() - Remove a portion of the array and replace it with something else
- (unset) casting