unset function in php
0 1955
Unset function of PHP is used to unset a variable means it destroy the value of an existing variable. Some interesting points about unset function are:
- It is an inbuilt function of PHP.
- The unset operation performed by unset function depends on the scope of the variable. If the variable has local scope means it is declared inside a user defined function then unset function destroy the local value of the variable.
- For destroying the value of global variable we have to use GLOBALS array of PHP.
- Unset function has no return type means it does not return any value.
- In case of passing multiple variable, unset function destroy the value all of them.
Syntax:
unset(variable1, ....,variableN);
Here,
Variable 1 to variableN are the variables which we want to unset.
Example 1:
<?php $var1="Coding Tag"; echo "The value is".$var1; unset($var1); // unset value of var1 echo "After unset the value is".$var1; ?>
Output:
Example 2:
<?php $var1="Coding Tag"; function check_val(){ $var1="inside function value"; echo "The value is".$var1; unset($var1); // unset value of var1 echo "After unset the value is".$var1; } check_val(); ?>
Output:
Example 3:
<?php error_reporting(0); $var1="Coding Tag"; function check_val(){ $val1=$GLOBALS['var1']; echo "inside function value is".$val1; unset($val1); // unset value of var1 echo "<br> After unset operation value is".$val1; } check_val(); echo "<br> global value is ".$var1; ?>
Output:
Share:
Comments
Waiting for your comments