取消设置变量组 [英] Unset group of variables

查看:52
本文介绍了取消设置变量组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能的重复:
你能在 PHP 中一次 unset() 多个变量吗?

$var1 = $var2 = $tvar3 = null;

像这样删除变量可以吗?

是否有更好的方法来取消设置几个变量?

Are there better ways to unset couple of variables?

推荐答案

unset() 是可变参数(即它接受任意数量的参数并将它们全部取消设置):

unset() is variadic (i.e. it takes any number of arguments and unsets them all):

unset($var1, $var2, $var3);

另请注意,unset() 与设置为 NULL 不同.使用 unset() 您实际上删除了变量,而将它们设置为 NULL 可以保持它们的定义,只是使用 NULL 的值".无论采用哪种方式,都会导致 isset() 在这些变量上返回 false,但它们在语义和技术上仍然不同.

Also note that unset() is not the same as setting to NULL. Using unset() you actually remove the variables, whereas setting them to NULL keeps them defined, just with a "value" of NULL. Doing it either way causes isset() to return false on those variables, but they're still semantically and technically different.

这是一个快速证明:

<?php

$x = NULL;
var_dump(array_key_exists('x', $GLOBALS)); // bool(true)

unset($x);
var_dump(array_key_exists('x', $GLOBALS)); // bool(false)

?>

这篇关于取消设置变量组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆