Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • PHP Tutorial
  • PHP Exercises
  • PHP Array
  • PHP String
  • PHP Calendar
  • PHP Filesystem
  • PHP Math
  • PHP Programs
  • PHP Array Programs
  • PHP String Programs
  • PHP Interview Questions
  • PHP GMP
  • PHP IntlChar
  • PHP Image Processing
  • PHP DsSet
  • PHP DsMap
  • PHP Formatter
  • Web Technology
Open In App
Next Article:
PHP | unset() Function
Next article icon

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
PHP | unset() Function

A

akash1295
Improve
Article Tags :
  • Web Technologies
  • PHP
  • PHP-function

Similar Reads

    PHP reset() Function
    The reset() function is an inbuilt function in PHP. This function is used to move any array's internal pointer to the first element of that array. While working with arrays, it may happen that we modify the internal pointer of an array using different functions like prev() function, current() functi
    2 min read
    PHP | settype() Function
    The settype() function is a built-in function in PHP. The settype() function is used to the set the type of a variable. It is used to set type or modify type of an existing variable. Syntax: boolean settype($variable_name, $type) Parameters: The settype() function accepts two parameters as shown in
    2 min read
    PHP | unlink() Function
    The unlink() function in PHP is used to delete a file from the filesystem. It does not delete directories. For that, you would need to use rmdir() or other methods.Syntax:unlink( $filename, $context )In this syntax:$filename: It is a mandatory parameter that specifies the filename of the file that h
    3 min read
    PHP trim() Function
    The trim() function in PHP is an inbuilt function which removes whitespaces and also the predefined characters from both sides of a string that is left and right. Related Functions: PHP | ltrim() Function PHP | rtrim() Function Syntax: trim($string, $charlist) Parameters:The function accepts one man
    2 min read
    PHP array_unshift() Function
    This inbuilt function of PHP is used to add one or more elements into an array and these elements are added to at the beginning of the array. All the elements that we add into the array are inserted in the same order, as they have been passed. They are numerically indexed starting from the 0th posit
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences