• PHP Video Tutorials

PHP - unset() Function


Advertisements

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

Useful Video Courses


Video

PHP Online Training

45 Lectures 9 hours

Malhar Lathkar

Video

Become PHP Facebook Developer: Password-Less Authentication

34 Lectures 4 hours

Syed Raza

Video

Ultimate PHP & MySQL Web Development Course & OOP Coding

84 Lectures 5.5 hours

Frahaan Hussain

Video

Learn PHP from Scratch

17 Lectures 1 hours

Nivedita Jain

Video

E Commerce Website Development In PHP With PDO

100 Lectures 34 hours

Azaz Patel

Video

PHP in Telugu

43 Lectures 5.5 hours

Vijay Kumar Parvatha Reddy

Advertisements