Updated April 1, 2023
Introduction to PHP unset()
The following article provides an outline on PHP unset(). The primary operation of the method unset() is to destroy the variable specified as an input argument for it. In other words, it performs a reset operation on the selected variable. However, its behavior can vary depending on the type of variable that is being targeted to destroy. This function is supported by version PHP4 onwards.
Syntax of PHP unset()
unset(mixed $selectedvar, mixed $selectedvar1,….., mixed $selectedvarN): void
- selectedvar: The mandatory argument for unset() method. At least one variable to unset, needs to be given as input argument for the method.
- selectedvarN: The optional parameter which can be given as input argument, to unset() method to reset it.
Use Cases for unset()
Given below are the different cases:
1. Applying unset() for local variable
When the local variable is passed to unset function, the function resets the variable.
Example:
Code:
<?php
$input = "I have value!!!";
echo "The value of 'input' before unset: " . $input . "<br>";
unset($input); //Applying unset() method on $input variable
echo "The value of 'input' after unset: " . $input;
?>
Output:
The value contained in the variable ‘input’ is destroyed on execution of the unset() method.
2. Applying unset for variable inside a function which is global variable
When user attempts to use Unset for a variable within a function and it is also defined as global variable, then unset() resets only the local one. The global one remains unaffected.
Example:
Code:
<?php
function Afunction()
{
$Avariable = 'local value';
echo "Within the function scope before unset: ".$Avariable."<br>";
global $Avariable;
unset($Avariable); //Deletes the local ‘Avariable’
echo "Within the function scope after unset: ".$Avariable."<br>";
}
$Avariable = 'Global Value'; //The global ‘Avariable’
echo "Out of the function scope before unset: ".$Avariable."<br>";
Afunction();
echo "Out of the function scope after unset: ".$Avariable."<br>";
?>
Output:
The local version of the variable ‘Avariable’ is destroyed where as the global version remains intact.
3. Applying unset for global variable within a function
If the variable within the function is also declared as global variable and user needs to destroy the global one, then it can be achieved using the array[$GLOBAL].
Example:
Code:
<?php
function Afunction()
{
$Avariable = 'local value';
echo "Within the function scope before unset: ".$Avariable."<br>";
global $Avariable;
unset($GLOBALS['Avariable']); //Resets the global ‘Avariable’
echo "Within the function scope after unset: ".$Avariable."<br>";
}
$Avariable = 'Global Value';
echo "Out of the function scope before unset: ".$Avariable."<br>";
Afunction();
echo "Out of the function scope after unset: ".$Avariable."<br>";
?>
Output:
The local version of the variable ‘Avariable’ is not affected by the execution of the unset function whereas the global version of the variable is set to null value.
4. Applying unset() for pass by reference variable
If unset() is called on a variable that is passed to the function as reference, unset() resets only the local one. The variable instance in the calling environment retains as it is.
Example:
Code:
<?php
function Afunction(&$Avariable) //’Avariable’ is the pass by reference
{
$Avariable = 'Internal value';
echo "Within the function scope before unset: ".$Avariable."<br>";
unset($Avariable); //Resets the local ‘Avariable’
echo "Within the function scope after unset: ".$Avariable."<br>";
}
$Avariable = 'External Value';
echo "Out of the function scope before unset: ".$Avariable."<br>";
Afunction($Avariable);
echo "Out of the function scope after unset: ".$Avariable."<br>";
?>
Output:
The unset() method called in the pass by reference variable ‘Avariable’ resets only the content of the variable in the local scope without affecting the content from the external scope.
5. Applying unset() for static variable
When a static variable is set as input argument to unset() method, the variable gets reset for the remaining command in the function scope after the function unset() is called.
Example:
Code:
<?php
function UnsetStatic()
{
static $staticvar;
$staticvar++;
echo "Before unset() method is called: $staticvar"."<br>";
//Deletes ‘staticvar’ only for the below commands within the scope of this ‘UnsetStatic’ function
unset($staticvar);
echo "after unset() method is called: $staticvar"."<br>";
}
UnsetStatic();
UnsetStatic();
UnsetStatic();
?>
Output:
The variable ‘staticvar’ is reset only for the commands followed after unset() method is called.
6. Applying unset() on an array element
The application of unset() method on an array element deletes the element from the array without exhibiting re-indexing operation.
Example:
Code:
<?php
$arrayinput = [0 => "first", 1 => "second", 2 => "third"];
Echo "The array elements, before unset:"."<br>";
Echo $arrayinput[0]." ". $arrayinput[1]." ". $arrayinput[2]." "."<br>";
//Unset operation is called on the second element of the array ‘arrayinput’
unset($arrayinput[1]);
Echo "The array elements, after unset:"."<br>";
Echo $arrayinput[0]." ". $arrayinput[1]." ". $arrayinput[2]." ";
?>
Output:
7. Applying unset() on more than one element at a time
The unset() method supports deleting multiple variable at once.
Example:
Code:
<?php
$input1 = "I am value1";
$input2 = "I am value2";
$input3 = "I am value3";
echo "The value of 'input1' before unset: " . $input1 . "<br>";
echo "The value of 'input2' before unset: " . $input2 . "<br>";
echo "The value of 'input3' before unset: " . $input3 . "<br>";
echo "<br>";
//Reseting input1, input2 and input3 together in single command
unset($input1,$input2,$input3);
echo "The value of 'input1' after unset: " . $input1."<br>";
echo "The value of 'input2' after unset: " . $input2."<br>";
echo "The value of 'input3' after unset: " . $input3."<br>";
?>
Output:
Recommended Articles
This is a guide to PHP unset(). Here we discuss the introduction to use cases for unset() along with examples for better understanding. You may also have a look at the following articles to learn more –