php

PHP unset() Function

The unset() function is used to reset any variable that has been defined earlier. Two types of variables are used in any programming language. One is a global variable and another is a local variable. The variable defined outside the function is called a global variable that is accessible from anywhere in the script. The variable defined inside the function is called a local variable that is accessible inside the function only.

If the unset function is used for the local and global variables, then it resets both variables locally. That means the value of the global variable remains unchanged that was defined before changing inside the function. The $GLOBALS array or global keyword can be used to unset the global variable permanently. The uses of this function to reset PHP variables have shown in this tutorial.

Syntax

This function can take multiple variables of different data types to delete and it returns nothing. The syntax of this function has given below.

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

The first argument of this function is mandatory. The other optional arguments can be used in this function to delete multiple variables.

Uses of unset() Function

The use of the unset() function to delete one or more variables has shown in this part of the tutorial.

Example-1: Reset the Global Variable

Create a PHP file with the following script to check the way of deleting a variable using the unset() variable. In the script, a float variable has been declared that has been deleted later using the unset() function. The variable has printed before delete and after deleting it.

<?php


//Initialize an floating value

$CGPA = 3.84;

//Display the value and dump value of the variable

echo "The value of the variable after set: $CGPA";

echo "<br/>The dump value of the variable after set: ";

var_dump($CGPA);

echo "<br/>";


//Unset the variable

unset($CGPA);


//Display the value and dump value of the variable after unset

echo "The value of the variable after unset: $CGPA";

echo "<br/>The dump value of the variable after unset:";

var_dump($CGPA);

echo "<br>";

?>

Output:

The following output will appear after executing the above script. The warning messages have printed in the output after deleting the variable.

Example-2: Reset the Global Variable Inside the Function

Create a PHP file with the following script to check how the global variable works when the variable is deleted inside the function. The global variable named $num has been declared in the script and 10 has been added with the variable and deleted the variable using the unset() function inside the function named unsetGlobal(). The $num has printed before and after calling the unsetGlobal() function.

<?php


//Declare an integer variable

$num = 10;


/*

Declare function to check

how the unset() function

works for gloabal variable inside function

*/


function unsetGlobal()

{

    global $num;

    //Add 10 with the global bariable

    $num = $num + 10;

    //Unset global variable

    unset($num);

}


//Print the global variable

echo "The value of the global variable before calling the function is $num";

//Call the function

unsetGlobal();

//Print the global variable again

echo "<br/>The value of the global variable after calling the function is $num";

?>

Output:

The following output will appear after executing the above script. According to the output, the global variable has not been destroyed inside the function.

Example-3: Reset the Global Variable with GLOBALS Array

The GLOBALS array is required to unset the global variables inside the function. Create a PHP file with the following script to unset a global variable inside a function by defining the global with GLOBALS array in the unset() function. The isset() function has been used in the script to remove the warming message after deleting the variable.

<?php


//Declare an integer variable

$num = 25;


/*

Declare function to check

how the unset() function

works for global variable inside the function

with GLOBALS[] array

*/


function unsetGlobal()

{

   unset($GLOBALS['num']);

}


//Print the global variable

echo "The value of the global variable before calling the function is $num";

//Call the function

unsetGlobal();

if(isset($num))

{

   //Print the global variable after unset

   echo "<br/>The value of the global variable after calling the function is $num";

}

else

   echo "<br/>The variable has destroyed."

?>

Output:

The following output will appear after executing the above script.

Example-4: Reset the Variable Pass by Reference

Create a PHP file with the following script to check the way of removing the variable that is passed by reference in the function. A string variable has been defined and passed by reference in the function named combineString(). The variable has been deleted after concatenating a string value inside the function. The variable has printed before and after calling the function and inside the function after delete.

<?php

/*Declare the function to combine string values and check

how the string variable works after

unsetting the reference variable inside the function

*/


function combineString(&$strVal)

{

   //Combine the string value

   $strVal .= " World";

   //Unset the variable

   unset($strVal);

   //Check the variable is set or not

   if(isset($strVal))

      echo "The value of the variable is <b>$strVal</b><br/>";

   else

       echo "The variable is undefined now.<br/>";

}


//Initialize a string variable

$strVal = "Hello";

//Print the variable

echo "The value of the variable is <b>$strVal</b><br/>";


//Call the function

combineString($strVal);

//Print the variable again after calling the function

echo "The value of the variable is <b>$strVal</b><br/>";

?>

Output:

The following output will appear after executing the above script. The output shows that the variable has been deleted locally and the global variable contains the modified value.

Example-5: Reset Static Variable

Create a PHP file with the following script to check how the static variable works after removing it inside the function. The unsetStaticVariable() function has been called three times to check the output of the static variables.

<?php

/*

Declare a function to check how static

variable works after unsetting the

variable inside the function

*/


function unsetStaticVariable()

{

   //Initialize an static variable

   static $counter = 1;

   //Increment the variable by 1

   $counter++;

   echo "<br/>The value of the counter before unset: $counter";

   //Destroy the variable

   unset($counter);

   //Check the variable is set or not

   if(isset($counter))

      echo "<br/>The value of the counter is now<b>$counter</b><br/>";

   else

      echo "<br/>The variable is undefined now.<br/>";

}


//Call the function multiple times

unsetStaticVariable();

unsetStaticVariable();

unsetStaticVariable();

?>

Output:

The following output will appear after executing the above script.

Conclusion

Different examples of unset() function have been shown in this tutorial to unset the global, reference, and static variables that will help the PHP users to know the purpose of this function properly.

About the author

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.