UNSET
Use : It Unset a given variable
But is this enough? I think it behaves more than that, depends on how and with respect to what you are using this function.For example you are destroying the local variable, global variable, static variable, or as a casting function. we will discuss later about the all given examples
HOW TO USE THIS
SYNTAX
void unset ($var1 [,$var2, ... ] )
unset is the function which will unset your variable.
$var1, $var2, ... are the variables which you wants to destroy. you can give any number of variables to destroy.
Return value:
It return nothing
BEHAVIOR OF UNSET FUNCTION
The behavior of unset() function depends on what type of variable you are attempting to destroy.
Three types of variable you want to destroy
1.GLOBAL VARIABLE
If a GLOBAL variable is unset() inside of a function, only the local variable is destroyed not the GLOBAL variable. The GLOBAL variable inside the function will retain the same value as before unset() was called.
Let's discuss it with the help of an example
EXAMPLE
<?php
function destroyMe()
{
global $var;
unset($var);
}
$foo = 'bar';//global variable
destroyMe();
echo $foo;
?>
OUTPUT
bar
The above function will only destroy the local variable
If you want to destroy global variable the you have do like this
<?php
function desctroyMe()
{
unset($GLOBALS['var']);
}
$var = "destroyME";
echo "Before destroy value is:".$var;
desctroyMe();
echo "After destroy value is:".$var;
?>
OUTPUT
Before destroy value is:destroyME
PHP Notice: Undefined variable: var in /home/tarun/temp.php on line 11
After destroy value is:
2. LOCAL VARIABLE
If you want to destroy the local variable then just use the function unset($var).
We will not discuss about this in detail because its easy to do, just try yourself.
3. STATIC VARIABLE
<?php
function unsetMe()
{
static $a;
$a++;
echo "Before unset a value is-->$a\n";
unset($a);
echo "$a\n";
static $a;
echo "After Unset a value is-->$a\n";
}
unsetMe();
unsetMe();
unsetMe();
?>
OUTPUT
PHP version PHP 5.5.9-1
Before unset a value is-->1
PHP Notice: Undefined variable: a in /home/tarun/temp.php on line 8
After Unset a value is-->1
Before unset a value is-->2
PHP Notice: Undefined variable: a in /home/tarun/temp.php on line 8
After Unset a value is-->2
Before unset a value is-->3
PHP Notice: Undefined variable: a in /home/tarun/temp.php on line 8
After Unset a value is-->3
You can see in the example that after unset is is again referencing to the same variable. surprised?. Yes it happen so when you will destroy the static variable, only refernece is destroyed to that variable, not the value.
Thats why when you have again get the reference using statement after unset
static $a;
It has linked you to the address of the variable, and printing the value of variable a.
4. AS A CASTING FUNCTION
When you are using unset() as a casting function then it will now originally destroy the value of the variable. here is the expaple which will described everything.
<?php
$name = 'Tarun';
var_dump((unset) $name);
var_dump($Tarun);
?>
OUPUT
NULL
string(5) "Tarun"
USEFUL LINKS
You can try here online
http://www.w3resource.com/php/function-reference/unset.php
Know how it behaves with array
http://stackoverflow.com/questions/6376702/php-unset-array-value
No comments:
Post a Comment