PHP unset() Function

What is PHP unset() Function?

PHP unset() function unsets a variable. After running this function, the variable lost its existence and its value in it. So, this function frees up the memory space that the variable occupies.

Beside unsetting a variable, the function has more capabilities. Actually, the function can unset-

  • a variable,
  • multiple variables at a time,
  • an array element,
  • an entire array,
  • an object property.

How PHP unset() function works:

Depending on attempting to destroy different types of variables based on its scopes and types, unset() function works differently-

  • For a Global Variable: If you call the unset() function in global scope (variable declaration outside a function), the function unset the variable for the rest in the script. Check example 1.
  • For a Local Variable: If you unset a global variable (global keyword is used before a variable inside a function to access a global variable) inside a function, the unset() function will only destroy the variable locally. The variable still retains its value in the calling environment. Check example 2.
  • For a Local Variable (using $GLOBAL array): If you unset a global variable inside a function using $GLOBAL array, it doesn’t destroy the variable inside the function locally but it destroys the variable outside the function. Check example 3.
  • For a Passed by Reference Variable: If you unset a variable that is passed by reference to a function, it only destroys the variable locally, but the variable retains its value in the calling environment. See example 4.
  • For a Static Variable: If you unset a static variable inside a function, the function only destroys the variable for the rest of the function, but, In the next call to the function, the variable restores its previous value as if it was not destroyed. Check example 5.
  • To unset an Array Element: To destroy an array element, just mention the array name with key. Note, after destroying the array element the key doesn’t exists. Check example 6.
  • To unset an Entire Array: To destroy an entire array, just mention the array name. Check example 7.
  • To unset an Object Property: To unset an property of an object, call it. Check example 8.

Syntax:

unset(variable1, variable2, variable3,…)

Parameters:

This function takes one required parameter and many optional parameters.

variable1 (Required): The variable to be unset.

variable2, variable3,… (Optional): More variable to be unset.

Syntax for single variable:

unset(variable1)

Syntax for multiple variables:

unset(variable1, variable2, variable3,…)

Syntax for array element:

unset(arrayName[elementKey])

Syntax for array:

unset(arrayName])

Syntax for object:

unset(object->objectProperty)

Return Values:

The function doesn’t return any value.

Examples:

Example 1:

<?php
   $language = "PHP";
   echo "Before unset() function, the value of 'language' is: " . $language . "<br>";
   unset($language);
   echo "Before unset() function, the value of 'language' is: " . $language;
?>

Output:

Before unset() function, the value of ‘language’ is: PHP

Warning: Undefined variable $language in D:\xampp\htdocs\php\unset.php on line 5
Before unset() function, the value of ‘language’ is:

Explanation:

As the variable $language is unset at line 4, the variable is no longer exists in the rest in the script. Hence, line 5 shows error “undefined variable $language”.

Example 2:

<?php
$language = "PHP";
echo "The value of 'language' in the calling environment before calling the function is: " . $language . "<br>";
function destroy_lang(){
    global $language;
    echo "The value of 'language' inside a function before calling unset functon is: " . $language . "<br>";
    unset($language);
    echo "The value of 'language' inside a function after calling unset functon is: " . $language . "<br>";
}
destroy_lang();
echo "The value of 'language' in the calling environment after calling the function is: " . $language;
?>

Output:

The value of ‘language’ in the calling environment before calling the function is: PHP
The value of ‘language’ inside a function before calling unset function is: PHP

Warning: Undefined variable $language in D:\xampp\htdocs\php\unset.php on line 8
The value of ‘language’ inside a function after calling unset function is:
The value of ‘language’ in the calling environment after calling the function is: PHP

Explanation:

Using global keyword, the variable $language is accessible inside the destroy_lang() function (line 6). After unsetting the variable (line 7), the variable is no longer accessible (line 8). But the variable retains its value outside the function after calling the function (line 11).

Exercise 3:

<?php
$language = "PHP";
echo "The value of 'language' in the calling environment before calling the destroy_lang_globally() function is: " . $language . "<br>";
function destroy_lang_globally(){
    global $language;
    echo "The value of 'language' inside a function before calling unset() functon is: " . $language . "<br>";
    unset($GLOBALS['language']);
    echo "The value of 'language' inside a function after calling unset() functon is: " . $language . "<br>";
}
destroy_lang_globally();
echo "The value of 'language' in the calling environment after calling the destroy_lang_globally() function is: " . $language . "<br>";
?>

Output:

The value of ‘language’ in the calling environment before calling the destroy_lang_globally() function is: PHP
The value of ‘language’ inside a function before calling unset() functon is: PHP
The value of ‘language’ inside a function after calling unset() functon is: PHP

Warning: Undefined variable $language in D:\xampp\htdocs\php\unset.php on line 11
The value of ‘language’ in the calling environment after calling the destroy_lang_globally() function is:

Explanation:

Using global keyword, the variable $language is accessible inside the destroy_lang() function (line 6). After unsetting the variable using $GLOBAL array (line 7), the variable is still available inside the function (line 8). But, the variable is destroyed outside the function (line 11) after calling the function.

Example 4:

<?php
$language = "PHP";
echo "The value of variable 'language' in the calling environment before calling the destroy_lang_locally() function is: " . $language . "<br>";
function destroy_lang_locally(&$language){
    echo "The value of variable 'language' (that is PASSED BY REFERENCE) inside a function before calling unset() function is: " . $language . "<br>";
    unset($language);
    echo "The value of variable 'language' (that is PASSED BY REFERENCE) inside a function before calling unset() function is: " . $language . "<br>";
}
destroy_lang_locally($language);
echo "The value of variable 'language' in the calling environment after calling the destroy_lang_locally() function is: " . $language . "<br>";
?>

Output:

The value of variable ‘language’ in the calling environment before calling the destroy_lang_locally() function is: PHP

Warning: Undefined variable $language in D:\xampp\htdocs\php\unset.php on line 5
The value of variable ‘language’ (that is PASSED BY REFERENCE) inside a function before calling unset() function is:

Warning: Undefined variable $language in D:\xampp\htdocs\php\unset.php on line 7
The value of variable ‘language’ (that is PASSED BY REFERENCE) inside a function before calling unset() function is:
The value of variable ‘language’ in the calling environment after calling the destroy_lang_locally() function is: PHP

Explanation:

The global variable $language (line 2) is passed to the custom function destroy_lang_locally() BY REFERENCE (line 9). After unsetting the variable (line 6) inside the custom function, the variable doesn’t exist inside the function (line 7). But, the variable still retains its value outside the function (line 10).

Exercise 5:

function num_count(){
    static $i = 0;
    $i = $i+1;
    echo "Before unset, value of the static variable i is: " . $i . "<br />";
    unset($i);
    echo "After unset, value of the static variable i is: " . $i . "<br />";
}
num_count();
num_count();
?>

Output:

Before unset, value of the static variable i is: 1

Warning: Undefined variable $i in D:\xampp\htdocs\php\unset.php on line 7
After unset, value of the static variable i is:
Before unset, value of the static variable i is: 2

Warning: Undefined variable $i in D:\xampp\htdocs\php\unset.php on line 7
After unset, value of the static variable i is:

Explanation:

In the first call of the function num_count() (line 9), the value of the static variable $i becomes 1 (line 5) and then the unset() function destroys the variable. Hence the script displays “Undefined variable $i” error. But, in the next call of the function (line 9), the value of the static variable $i becomes 2 as the static variable restores it previous value 1.

Exercise 6:

<pre>
<?php
$language = ["PHP", "Python", "Java", "C++"];
print_r($language);
unset($language[2]);
echo "The array after unset() is: <br />";
print_r($language);
?>
</pre>

Output:

Array
(
    [0] => PHP
    [1] => Python
    [2] => Java
    [3] => C++
)
The array after unset() is: 
Array
(
    [0] => PHP
    [1] => Python
    [3] => C++
)

Explanation:

The function unset($language[2]) destroys the third element. note after unsetting the array element, the array doesn’t contain that element key.

Exercise 7:

<?php
$language = ["PHP", "Python", "Java", "C++"];
unset($language);
print_r($language);
?>

Output:

Warning:  Undefined variable $language in D:\xampp\htdocs\phpExercise\7. unset.php on line 75

Explanation:

The unset() function destroys the entire array, so attempting to printing the array shows “Undefined variable” error.

Exercise 8:

<pre>
<?php
class car{
    public $brand = "Toyota";
    public $color = "White";
}

$carObject = new car();
unset($carObject -> brand);
print_r($carObject);
?>
</pre>

Output:

car Object
(
    [color] => White
)

Explanation:

The unset() function destroys the  property “brand”. So, printing the function after unset() function displays the remaining one property.

Practical Usages of unset() Function:

  • Freeing up memory space allocated for variables, the function efficiently manages memory.
  • This function is useful to remove sensitive session data.
  • This function is useful to remove unnecessary variables. For example, an unnecessary huge pile of information that were loaded from database or from external file.
  • This function is useful to remove any sensitive data that is no longer safe to store.

PHP Version Support:

PHP 4, PHP 5, PHP 7, PHP 8

Summary: PHP unset() Function

PHP unset() function is one of most frequently used PHP variable handling type functions. You’ll need to use it in memory management, unsetting session data etc.

Reference:

https://www.php.net/manual/en/function.unset.php