unset(): removes a variable from memory

Remove variables or array elements with unset()

unset() removes a variable or selected array element. Removing an array element does not automatically reindex numeric keys; use array_values() when a consecutive numeric index is required afterward.

<?php
$items = ['red', 'green', 'blue'];
unset($items[1]);
$items = array_values($items);
print_r($items);
?>
We can destroy variable by using unset function.
Unset(variable)
Here is an example.
$my_var='plus2net';
echo " before unset : ".$my_var;
echo "<br>";
unset($my_var);
echo "after unset : " .$my_var;
The output is here
before unset : plus2net
after unset  :
Unset function will remove local variable only. For example it can remove only variable inside the function and it does not affect outside the function. Here is one example to understand how unset function work inside function.
$my_var='plus2net';

function unset_var()
{
global $my_var;
echo "Before unset and inside function :".$my_var."<br>";
unset($my_var);
echo "after unset and inside function  :".$my_var."<br>";
}
echo "Outside function before using function : ".$my_var."<br>";
unset_var();
echo "Outside function after using function : ".$my_var."<br>";
Here is the output
Outside function before using function : plus2net
Before unset and inside function :plus2net
after unset and inside function :
Outside function after using function : plus2net
Declaring variables Cookies to store Infomation Getting Type of Variable var_dump() isset() empty()


Subscribe to our YouTube Channel here



plus2net.com











PHP video Tutorials
✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer