Php unset() method

Php unset() method is used to delete the variable. when you unset the variable then variable is deleted and it release the occupied memory by the variable.
unset() method is used to delete the array element and object property also.

Syntax:-


<?php
unset($variable_name);
?>

Example:- Suppose, You have an employee_name variable and you unset this variable and after that print, this variable then shows the Undefined variable: employee_name.


<?php
$employee_name="John";
unset($employee_name);
echo $employee_name;
?>
Notice: Undefined variable: employee_name

Try it Yourself

Delete the Array element through unset() method

If you want to delete the array then use unset() method.

Syntax:-


<?php
unset($array_element);
?>

Example:- Suppose, You have an emp_arr array and you want to unset this array element which is on zero position then use unset() method.


<?php
$emp_arr=array("John","Tom","Mathew");
unset($emp_arr[0]);
print_r($emp_arr);
?>
Output:- Array ( [1] => Tom [2] => Mathew )

Try it Yourself