ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel
  • »
  • Technology»
  • Computers & Software»
  • Computer Science & Programming»
  • Programming Languages

Learn how to use PHP unset Function

Updated on October 24, 2011


If you ever stuck in a condition where you want to free up memory by destroying any variable, group of variables or elements of array, then, you can do so by using the "unset()" function. This PHP function is a member of variable handling functions in PHP. But as per the official documentation (Notes) on PHP, this is a language construct and not a function, it cannot be called using variable functions. Some language constructs can be called without parenthesis.

List of Contents

  1. Most Common Language Constructs,
  2. Usage and Description unset Function,
  3. Syntax of Unset Function,
  4. Parameters,
  5. Practical Examples showing the usage of unset function,
  6. Resolve Your Queries.

PHP Unset Function
PHP Unset Function

Most Common Language Constructs

  1. echo(),
  2. print(),
  3. unset(),
  4. isset(),
  5. empty(),
  6. include(),
  7. require().

Usage and Description

This PHP function destroys a variable specified in it's parameter. You can destroy as many variables you want by submitting the list in comma-separated values. Well, This function does what it intends to but have some limitations related to its scope of working, Which is listed below.

  • If you'll destroy a variable inside a function using "unset" function, then, destroyed variable will not be available in rest/remaining part of function unless declared again but it'll still remain in memory i.e. outside the scope of function (If declared there previously).

Sample Example of Unset Function

function destroy_myVariable() 
    global $myVariable;

$myVariable = 'This is my Garbage Data';
echo $myVariable;

	This is my Garbage Data


Here, below is the syntax of unset function PHP. In this syntax, "..." three dots are indicating that you can add as many parameters as argument in this function.

Synatx of unset PHP Function

void unset ( mixed $var [, mixed $... ] );


This function does not have limit of parameters but it only accepts few kinds of values which is described below.

  • "$var" - The "php variable" or "element of any array" that needs to be destroyed.

Examples of unset Function

Here, Below are the examples showing the usage of unset function in PHP Scripts.

SImple Usage of unset function


// destroy a single variable
$myVariable="My Data";
echo "<b>OUTPUT 1:</b> ~~~| ".$myVariable." |~~~<br />";
//Above Echo Statement Will also Issue a notice referencing to destroyed Variable

$myArray=array("one"=>"gay", "six"=>"lesbians", "three"=>"Normal Humans");

// destroy a single element of an array
echo "<b>OUTPUT 2: </b>";
echo "<br/>";

// destroy more than one elements of array (You can use variables too)
unset($myArray['one'], $myArray['six']);
echo "<b>OUTPUT 3: </b>";
echo "<br/>";


Notice: Undefined variable: myVariable in C:\Program Files\xampp\htdocs\online\phptest.php on line 6
OUTPUT 1: ~~~| |~~~
OUTPUT 2: Array ( [six] => lesbians [three] => Normal Humans )
OUTPUT 3: array(1) { ["three"]=> string(13) "Normal Humans" } 


Ask For Help

If you have queries regarding this function or about any part of this tutorial, the, you can ask about it in comments section below. I will try my best to resolve it in a simplest way.


    0 of 8192 characters used
    Post Comment

    No comments yet.