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

Php unset() method – Interview Questions

Q 1: What is unset() in PHP?

Ans: It removes a variable from memory.

Q 2: What happens after unsetting a variable?

Ans: The variable becomes undefined.

Q 3: Can unset() remove array elements?

Ans: Yes.

Q 4: Does unset() return a value?

Ans: No.

Q 5: Is unset() useful for freeing memory?

Ans: Yes.

Php unset() method – Objective Questions (MCQs)

Q1. unset() is used to ______.






Q2. After using unset($var), $var becomes ______.






Q3. Can unset() remove array elements?






Q4. unset() returns a value?






Q5. unset() can be used to destroy ______.






Related C# Dictionary Topics