unset function in php || php functions

unset function in php

Last Updated by Pooja Chikara 06-Apr-20

0 218


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:

  1. It is an inbuilt function of PHP.
  2. 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.
  3. For destroying the value of global variable we have to use GLOBALS array of PHP.
  4. Unset function has no return type means it does not return any value.
  5. 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:

unset function in php


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:

unset function in php


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:

unset function in php


Share:


Leave a Reply


Comments
    Waiting for your comments