Php Unset Function Tutorial With Examples

03-Apr-2023

.

Admin

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:


Syntax

unset(variable)

Example : Remove Variable using unset

<?php

$variable = "John Doe";

echo "unset not applied : ".$variable;

echo "<br>";

unset($variable);

echo "unset applied : " .$variable;

?>

/* output:

unset not applied : John Doe

unset applied :

*/

Remove Array Items using unset

<?php

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

unset($names[2]);

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

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

}

?>

/* output:

0 -> john

1 -> anna

3 -> henry

4 -> vernoica

*/

It will help you....

#PHP