PHP Variable Handling unset() Function



The PHP Variable Handling unset() function is used to to delete a variable. Unsetting a variable deletes it from memory and prevents it from being used again. If you use an unset variable, it will have no value. Unsetting a variable within a function removes only the local copy. The original variable outside the function will remain unchanged. If you want to remove a global variable from a function, use the $GLOBALS array.

If you pass a variable by reference and then unset it within a function, just the local copy is destroyed. The original variable's value will remain constant.

When a static variable is unset inside a function, it is only removed for that function call. When you call the function again, the static variable retains its prior value.

This function returns nothing. It just delete variables from memory.

Syntax

Below is the syntax of the PHP Variable Handling unset() function −

void unset ( mixed $var, mixed ...$vars )

Parameters

Below are the parameters of the unset() function −

  • $var − It is the variable you want to delete.

  • $vars − More variables you want to delete. It is an optional parameter.

Return Value

The unset() function does not return anything. It just deletes the given variables.

PHP Version

First introduced in core PHP 4, the unset() function continues to function easily in PHP 5, PHP 7, and PHP 8.

Example 1

First we will show you the basic example of the PHP Variable Handling unset() function to unset a variable. We will show the variable before and after deleting the variable.

<?php
   // Define a variable
   $a = "Welcome TutorialsPoint!";
   echo "The value of 'a' before unset: " . $a . "\n";
   unset($a);
   echo "The value of 'a' after unset: " . $a;
?>

Output

Here is the outcome of the following code −

The value of 'a' before unset: Welcome TutorialsPoint!
The value of 'a' after unset:

Example 2

In the below PHP code we will try to use the unset() function and unset a global variable inside a function with the help of the $GLOBALS array. A global variable, $globalVar, is declared, and the removeGlobalVar() function deletes it using $GLOBALS.

<?php
   // Define a global variable
   $globalVar = "I am global"; 

   function removeGlobalVar() {
      // Unset the global variable 
      unset($GLOBALS['globalVar']); 
   }

   removeGlobalVar();
   // Check if the variable exists
   echo isset($globalVar) ? "Global variable exists." : "Global variable is deleted."; 
?> 

Output

This will generate the below output −

Global variable is deleted

Example 3

If a variable that is PASSED BY REFERENCE is unset() inside of a function, only the local variable is destroyed. The variable in the calling environment will retain the same value as before unset() was called. Following example demonstrates this:

<?php
   function destroy_c($v) {
      unset($c);
      $c = "heloo";
   }
   $c = 'TutorialsPoint';
   echo "$c\n";

   destroy_c($c);
   echo "$c\n";
?> 

Output

This will create the below output −

TutorialsPoint
TutorialsPoint
php_variable_handling_functions.htm
Advertisements