Open In App

PHP | unset() Function

Last Updated : 09 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In PHP, the unset() function is used to destroy a variable, array element, or object. Once a variable or array element is “unset”, it is completely removed from memory, and it no longer exists in the program. This function is essential for freeing up memory by removing unwanted or unnecessary data during the execution of a script.

Syntax:

unset($variable);
  • $variable: The variable or array element to be removed.
  • Return Value: This function does not return any value. It simply removes the specified variable or array element from memory.

How unset() Works?

  • Unsetting a Single Variable: When you call unset() on a single variable, the variable is destroyed, and it can no longer be accessed.
  • Unsetting Array Elements: When you use unset() on an array element, that specific element is removed from the array. However, the array itself remains intact, just without that particular element.
  • Unsetting Multiple Variables: You can unset multiple variables in a single call to unset(). Each variable will be removed independently.
  • Unsetting Objects: When you unset an object, the object reference is removed, and PHP will automatically free up memory used by the object when there are no more references to it.

Implementing the unset() Function

Below we have shown the implementation of the unset() function:

Unsetting a Single Variable

PHP
<?php
$name = "Anjali";
echo $name . "\n";  
unset($name); 
echo $name;  
?>

Output:

Anjali
Warning: Undefined variable $name in /tmp/dkZiYIQ6cZ/main.php on line 6

In this example:

  • The variable $name is first assigned a value, and it is printed.
  • After calling unset($name), the variable $name is removed, and any attempt to use it will result in an “undefined variable” error.

Unsetting Multiple Variables

PHP
<?php
$a = 10;
$b = 20;
$c = 30;
unset($a, $b); 
echo $a;  
echo $b;  
echo $c;  
?>

Output:

Warning: Undefined variable $a in /tmp/UfFD3o8CQ4/main.php on line 8

Warning: Undefined variable $b in /tmp/UfFD3o8CQ4/main.php on line 9
30

In this example:

  • The variables $a, $b, and $c are set.
  • After calling unset($a, $b), both $a and $b are removed.
  • $c remains unaffected and can still be used.

Unsetting an Array Element

PHP
<?php
$fruits = array("apple", "banana", "cherry");
unset($fruits[1]);  
print_r($fruits); 
?>

Output
Array
(
    [0] => apple
    [2] => cherry
)

In this example:

  • The variables $a, $b, and $c are set.
  • After calling unset($a, $b), both $a and $b are removed.
  • $c remains unaffected and can still be used.

Unsetting an Object

PHP
<?php
class Person {
    public $name;
    public $age;

    public function __construct($name, $age) {
        $this->name = $name;
        $this->age = $age;
    }
}
$person = new Person("GFG", 25);
echo $person->name;  

unset($person); 
echo $person->name; 
?>

Ouput:

GFG
Warning: Undefined variable $person in /tmp/z2Xjdh9gm3/main.php on line 18

Warning: Attempt to read property "name" on null in /tmp/z2Xjdh9gm3/main.php on line 18

In this example:

  • The $person object is created with properties name and age.
  • After calling unset($person), the $person object is removed, and trying to access its properties will cause an error.

Conclusion

The unset() function in PHP is a simple but powerful tool for managing memory and cleaning up variables, array elements, or objects that are no longer needed. By using unset() efficiently, you can improve the performance of your application, especially when working with large data structures or sensitive information. Always remember that unset() does not return any value, and once a variable is unset, it cannot be used unless it is redefined.



Next Article

Similar Reads