Computer >> Computer tutorials >  >> Programming >> PHP

PHP Unsetting References


Introduction

It is possible to break binding between the content and variable by using unset() function. The unset() function doesn't destroy the content but only decouples variable from it.

Example

<?php
$a=10;
$b=&$a;
echo "before unsetting : ", $a, " " ,$b, PHP_EOL;
unset($b);
echo "after unsetting :" . $a . " ";
$b=20;
echo $b;
?>

Output

After unsetting, $b can be used as normal vaiable

before unsetting : 10 10
after unsetting : 10 20

Reference can also be removed by assigning variable to NULL

Example

<?php
$x=100;
$y=&$y;
echo "x and y are references ", $x, " " ,$y, PHP_EOL;
$y=NULL;
$x=200;
echo "x: ", $x . " y: " ,$y, PHP_EOL;
?>

Output

Result of above script is as follows

x and y are references 100
x: 200 y: