小白教程
所有教程
关于
Search
172.69.6.102
172.69.6.102
参数设置
贡献
退出
操作
编辑
移动
保护
信息
历史
删除
查看“Php unset function”的源代码
本页内容
上一节:
Php_unserialize_function
下一节:
Php_var_dump_function
因为以下原因,您没有权限编辑本页:
您请求的操作仅限属于该用户组的用户执行:
用户
您可以查看和复制此页面的源代码。
{{DISPLAYTITLE:unset}}[[Category:php variable handling functions|33]] = PHP unset() 函数 = [[php variable handling functions|PHP 可用的函数]] '''unset()''' 函数用于销毁给定的变量。 PHP 版本要求: PHP 4, PHP 5, PHP 7 === 语法 === <sample title="" desc="" lang="php" hererun="1"> void unset ( mixed $var [, mixed $... ] ) </sample> 参数说明: * $var: 要销毁的变量。 === 返回值 === 没有返回值。 == <sample title="" desc="" lang="php" hererun="1"> <?php // 销毁单个变量 unset ($foo); // 销毁单个数组元素 unset ($bar['quux']); // 销毁一个以上的变量 unset($foo1, $foo2, $foo3); ?> </sample> 如果在函数中 unset() 一个全局变量,则只是局部变量被销毁,而在调用环境中的变量将保持调用 unset() 之前一样的值。 <sample title="" desc="" lang="php" hererun="1"> <?php function destroy_foo() { global $foo; unset($foo); } $foo = 'bar'; destroy_foo(); echo $foo; ?> </sample> 输出结果为: <sample title="" desc="" lang="php" hererun="1"> bar </sample> 如果您想在函数中 unset() 一个全局变量,可使用 $GLOBALS 数组来实现: <sample title="" desc="" lang="php" hererun="1"> <?php function foo() { unset($GLOBALS['bar']); } $bar = "something"; foo(); ?> </sample> 如果在函数中 unset() 一个通过引用传递的变量,则只是局部变量被销毁,而在调用环境中的变量将保持调用 unset() 之前一样的值。 <sample title="" desc="" lang="php" hererun="1"> <?php function foo(&$bar) { unset($bar); $bar = "blah"; } $bar = 'something'; echo "$bar\n"; foo($bar); echo "$bar\n"; ?> </sample> 以上例程会输出: <sample title="" desc="" lang="php" hererun="1"> something something </sample> 如果在函数中 unset() 一个静态变量,那么在函数内部此静态变量将被销毁。但是,当再次调用此函数时,此静态变量将被复原为上次被销毁之前的值。 <sample title="" desc="" lang="php" hererun="1"> <?php function foo() { static $bar; $bar++; echo "Before unset: $bar, "; unset($bar); $bar = 23; echo "after unset: $bar\n"; } foo(); foo(); foo(); ?> </sample> 以上例程会输出: <sample title="" desc="" lang="php" hererun="1"> Before unset: 1, after unset: 23 Before unset: 2, after unset: 23 Before unset: 3, after unset: 23 </sample> [[php variable handling functions|PHP 可用的函数]]
返回至“
Php unset function
”。
上一节:
Php_unserialize_function
下一节:
Php_var_dump_function