• PHP Video Tutorials

PHP - unset() Function



Definition and Usage

The function unset() destroys the specified variables. The behavior of unset() inside of a function can vary depending on what type of variable you are attempting to destroy.

  • If a globalized variable is unset() inside of a function, only the local variable is destroyed.

  • The variable in the calling environment will retain the same value as before unset() was called.

Syntax

void unset ( mixed $var , mixed $vars )

Parameters

Sr.No Parameter & Description
1

var

Mandatory.he variable to be unset.

2

vars

Optional.Further variables.

Return Values

This function does not return any value.

Dependencies

PHP 4 and above

Example

Following example demonstrates use of unset() function −

<?php
   $a = "Welcome TutorialsPoint!";
   echo "The value of 'a' before unset: " . $a . "<br>";
   unset($a);
   echo "The value of 'a' after unset: " . $a;
?>

Output

This will produce following result −

The value of 'a' before unset: Welcome TutorialsPoint!
The value of 'a' after unset:

Example

Following example demonstrates case when globalized variable is unset() inside of a function −

<?php
   //globalized variable is unset() inside of a function
   function destroy_a() {
      global $a;
      unset($a);
   }

   $a = 'Welcome TutorialsPoint!';
   destroy_a();
   echo "global unset inside function =".$a;
?>

Output

This will produce following result −

global unset inside function =Welcome TutorialsPoint!

Example

If a variable that is PASSED BY REFERENCE is unset() inside of a function, only the local variable is destroyed. The variable in the calling environment will retain the same value as before unset() was called. Following example demonstrates this −

<?php
   function destroy_c($v) {
      unset($c);
      $c = "heloo";
   }
   $c = 'TutorialsPoint';
   echo "$c<br>";

   destroy_c($c);
   echo "$c<br>";
?>

Output

This will not produce result −

TutorialsPoint
TutorialsPoint
php_variable_handling_functions.htm
Advertisements