PHP 取消引用

phpserver side programmingprogramming更新于 2025/8/19 16:07:17

简介

可以使用 unset() 函数解除内容和变量之间的绑定。unset() 函数不会销毁内容,而只是将变量与内容解耦。

示例

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

输出

解除锁定后,$b 可以作为普通变量使用

before unsetting : 10 10
after unsetting : 10 20

也可以通过将变量赋值为 NULL 来删除引用

示例

<?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;
?>

输出

上述脚本的结果如下

x and y are references 100
x: 200 y:

相关文章