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:
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....
- How To Use Php Conditional Statement Example
- How to Check if String Contains Regex in PHP?
- PHP Array_column() Function Example
- PHP Generate List of Random Numbers Between 0 and 100 Example
- How to User Login with Facebook in PHP?
- How to Explode Every Character into Array Using PHP Example?
- PHP Form Validation Tutorial
- PHP Convert XML to JSON Example