- HubPages»
- Technology»
- Computers & Software»
- Computer Science & Programming»
- Programming Languages
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
- Most Common Language Constructs,
- Usage and Description unset Function,
- Syntax of Unset Function,
- Parameters,
- Practical Examples showing the usage of unset function,
- Resolve Your Queries.
Most Common Language Constructs
- echo(),
- print(),
- unset(),
- isset(),
- empty(),
- include(),
- 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 */ ?>
Useful Tutorial
- Learn About PHP function syntax
In this PHP Tutorial, I'll explain some of the basics about PHP function Syntax (or I say syntax of PHP function), Which one should have in understanding it without any confusion.
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.