Learn how to use PHP unset Function

Introduction

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

<?php
function destroy_myVariable() 
{
    global $myVariable;
    unset($myVariable);
}

$myVariable = 'This is my Garbage Data';
destroy_myVariable();
echo $myVariable;

//OUTPUT OF ABOVE PHP CODE WILL BE
/*
	This is my Garbage Data
*/
?> 

Syntax

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 $... ] );

Parameters

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

<?php

// destroy a single variable
$myVariable="My Data";
unset($myVariable);
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
unset($myArray['one']);
echo "<b>OUTPUT 2: </b>";
  print_r($myArray);
echo "<br/>";

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

//OUTPUT OF ABOVE CODE WILL BE
/*

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.

More by this Author


Comments

No comments yet.

    Sign in or sign up and post using a HubPages Network account.

    0 of 8192 characters used
    Post Comment

    No HTML is allowed in comments, but URLs will be hyperlinked. Comments are not for promoting your articles or other sites.


    Click to Rate This Article
    working