The PHP unset() function can be written as the below syntax.

<?php

  $var = "CodedTag.com Tutorials";

  echo $var; // It shows the variable result

  unset( $var ); // destroy the $var variable

  echo $var; // show an error

?>

The output:

CodedTag.com Tutorials

Notice: Undefined variable: var in D:\xampp\htdocs\site-1\index.php on line 5

The unset() is a built-in PHP function used to destroy or delete the given variable. This function doesn’t return anything because it is a void type, which means there is no return inside it.

Let’s move to the following part to understand the required parameters.

PHP unset() Function Parameters

As I mentioned, it used to unset a specific variable. It takes at least one argument as a required parameter, and it can take more than two arguments.

  • Required argument (mixed data type)
  • Unlimited optional arguments (mixed data type)

The basic expression can be written as the below.

unset( $required, $var1, $var2, $var3, ... );

Anyway, let’s see how it does work.

How the PHP unset() Function Works

The sequence of the PHP script is reading the statements line by line. So when you define a variable, you already store a new data in a memory.

And these data can be defined by addresses. So each variable contains a value with a data type and each data type stored in the memory according to number of bytes.

The bytes that already reserved have addresses and when you invoke these variable the PHP is calling these variables by these addresses automatically.

I mean, when you go to use the unset() Function in anywhere, the interpreter deletes the address with its data from the memory according to the variable identifier. To understand that, take a look at the following figure.

php unset function

Anyway, let’s move to the examples.

PHP unset() Function Examples

Destroy data of many variables in the same time.

<?php
  $var1 = "Peter";
  $var2 = "Ahmed";
  $var3 = "Kamel";
  unset( $var1, $var2, $var3 );
?>

Also, you can delete or destroy an array element.

<?php
  $array = array(
    "model" => "91",
    "name"  => "BMW",
    "motor" => "Viber 1684"
  );
 
  unset($array["motor"]);
  print_r($array);
?>

The output:

Array
(
    [model] => 91
    [name] => BMW
)

Unset, a variable scope.

<?php
 $var = "PHP Tutorials";
 function callback() {
   global $var;
   unset($var);
 }
 callback();
 echo $var; // PHP Tutorials
?>

It will destroy the variable only locally, and it will not affect in the variable scope.

Unset, a static variable in the function.

<?php
 
 function callback() {
   static $a;
   $a++;
   echo $a;
}
callback(); // 1
callback(); // 2
callback(); // 3
?>

When you use unset() there, it will destroy the last stored value for the static property. For example.

<?php
 function callback() {
   static $a;
   $a++;
   echo $a;
   unset($a);
   echo $a;
}

callback(); 
// before unset: 1 after unset: undefined variable

callback(); 
// before unset: 2 after unset: undefined variable

callback(); 
// before unset: 3 after unset: undefined variable
?>

Using unset() as a casting.

<?php
 $string = "Hello World";
 var_dump( (unset) $string ); // NULL 
 echo  $string; // Hello World
?>

Wrapping Up

The PHP unset() is a predefined function used to fully delete or destroy the variable values from the storage.

The basic expression as the below.

unset( $required, $var1, $var2, $var3, ... );

It accepts unlimited variables as parameters.