Php Unset Function Tutorial With Examples




Hi Dev,

This article will give you example of php unset funtion example. we will learn unset function example in php. The unset() function is used to destroy the given variable. If you are trying to unset() global variable within the function, then the local variable within the function will be destroyed. This function is mostly used for local variables.

The unset function in php can be used to remove array items. We can take help of array key to delete an array’s item.

Here I will give example of php unset function. So let's see the bellow example:



Example : Remove Variable using unset


$variable = "John Doe";

echo "unset not applied : ".$variable;

echo "<br>";


echo "unset applied : " .$variable;


/* output:

unset not applied : John Doe

unset applied :


Remove Array Items using unset


$names = array("john", "anna", "sam", "henry", "vernoica");


while (list ($key, $val) = each ($names)) {

echo "$key -> $val <br>";



/* output:

0 -> john

1 -> anna

3 -> henry

4 -> vernoica


It will help you....