Using PHP unset Function to Destroy a Variable, With Examples

PHP unset()

Here’s a short article on a useful PHP feature – the unset construct allows you to destroy a variable, making it unavailable for use.

There are a few reasons why you might want to unset a variable:

  • Making sure you don’t accidentally use a variable you don’t want to use in a certain context or scope
    • Cleaning up variables after a loop
    • Make a global variable unavailable within a function or scope
  • Remove an element from an Array
  • Remove an object attribute

Read on to see how it’s done.

PHP unset() Syntax

Here’s the syntax for calling unset:

unset($variable1, $variable2, $variable3...)

Note that:

  • One or more variables can be supplied, separated by commas
  • The variable being unset will depend on the scope unset is called from
    • See below for examples
  • Using unset is not intended as a means of freeing up memory by removing variables.
  • No values are returned by unset.
  • No errors are thrown if the variables passed to unset do not exist

To check if a variable is set, use the isset() function!

unset Examples and Behaviour

unset behaves differently depending on where it is used and how the variable it is used on was defined.

Global Variables

unset() when called from within a function will only destroy the given variable in that function’s scope. If a global variable is passed to unset(), it will become unavailable locally, but the global variable will remain.

$var = 'Oh, Linux'; // Global variable defined

function myFunction(){
    global $var; // Global variable made available within function
    unset($var); // Will only unset the variable within the function
}

To unset a global variable from within a function, it must be referred to within the $GLOBALS array:

function myFunction(){
    unset($GLOBALS['var']); // Unsetting the global variable $var from within a function
}

Variables Passed By Reference

If a variable is passed to a function by reference, unsetting the variable within the function will likewise only destroy it within the function – the original variable will still exist outside of the function.

function myFunction(&$ref){
    unset($ref); // Will only unset the variable within the function
}

$var = 'Oh, Linux';

myFunction($var);

echo $var; // Original variable which was passed to the function by reference still exists

Static Variables

Unsetting a static variable within a function will only apply for the rest of that specific execution of the function. It will not affect or remain for other calls to that function.

function myFunction(){
    static $var = "The original\n";
    echo $var;
    unset($var);
    $var = "The imposter";
    echo "After unset: $var\n";
}

myFunction();
myFunction();

Which will output:

The original
After unset: The imposter
The original
After unset: The imposter

As you can see, despite being unset, the variable $var is returned to its expected state when myFunction() is called multiple times.

Removing Element from Array

unset() is particularly useful for removing elements from an array:

$myArray = ['pears', 'apples', 'oranges'];
unset($myArray[2]);
print_r($myArray); // $myArray is now ['pears', 'apples']

Removing Object Properties

Similarly, object properties can be removed with unset():

$apple = (object) [
    'colour' => 'red',
    'flavour' => 'delicious',
    'ripe' => 'yes',
]; // Creating an object from an associative array
unset($apple->flavour); // Unset an attribute from the object
print_r($apple); // The 'flavour' attribute is now missing from the object

 

SHARE:
nv-author-image

Brad Morton

I'm Brad, and I'm nearing 20 years of experience with Linux. I've worked in just about every IT role there is before taking the leap into software development. Currently, I'm building desktop and web-based solutions with NodeJS and PHP hosted on Linux infrastructure. Visit my blog or find me on Twitter to see what I'm up to.

Leave a Reply

Your email address will not be published. Required fields are marked *