PHP unset()

王林
王林Original
2024-08-29 12:53:091197browse

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.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

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.

PHP unset()

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.

PHP unset()

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.

PHP unset()

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.

PHP unset()

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.

PHP unset()

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:

PHP unset()

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:

PHP unset()

Note: (unset) casting is not same as the function unset(). (unset)casting is used only as cast of type NULL whereas unset() method alters the variable. unset() is a language construct and hence is not supported by variable function. unset() method can be used to reset object properties that are visible in the current scope except ‘$this’ variable within any object method. In order to perform unset operation on the object properties that are not accessible in the current scope, an overloading method __unset() needs to be declared and called.

The above is the detailed content of PHP unset(). For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:PHP MD5()Next article:PHP MD5()