PHP unset() Function

PHP unset() function is “used to unset a specified variable.” If unset() is called inside a user-defined function, it unsets the local variables. To unset a global variable inside the function, the $GLOBALS array needs to be used.


unset ($var1, $var2.... )


$var1, $var2: It is the variable which is needed to be unset. You can pass as many variables as possible.

Return value


Visual RepresentationVisual Representation of PHP unset() Function

Example 1: How to Use the unset() Function


$var = 'Millie Bobby Brown'; 

echo " Before unset : ".$var ."\n";


echo "After unset : " .$var;


Before unset : Millie Bobby Brown
After unset : 

Example 2: unset the global variable


$var = 'Millie Bobby Brown';

function eleven()
echo "Ouside function before using unset: ".$var."\n";

echo "Outside function after using unset: ".$var; 


Ouside function before using unset: Millie Bobby Brown
Outside function after using unset: 
To check if the variable is set in PHP, use the PHP isset() function.

That’s it.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.