PHP unset()函数
PHP unset() 函数 unset()函数是PHP的预定义变量处理函数,用于取消设置指定的变量。换句话说…
PHP unset() 函数
unset()函数是PHP的预定义变量处理函数,用于取消设置指定的变量。换句话说,“unset()函数破坏了变量”。
该函数的行为在用户定义函数内部有所不同。如果在函数内部未设置全局变量,则unset()将在本地销毁它,并保留最初为外部提供的相同值。使用$GLOBALS数组销毁函数内部的全局变量。
注意:如果使用unset() 函数未设置变量,则认为设置的时间不会太长。
句法
unset (mixed variable, ….)
混合表示参数可以是多种类型。
参量
这个函数接受一个或多个参数,但ATLEAST一个参数必须在这个函数中进行传递。
变量(必填)-该参数是必须传递的,因为它包含该变量,需要将其取消设置。
…-要设置的更多变量数量是可选的。
返回值
unset()函数不返回任何值。
例子
下面给出一些示例,通过这些示例您可以了解unset()函数:
范例1:
<?php //variable $website is initialized $website='javatpoint.com'; //display the name of the website echo 'Before using unset() the domain name of website is : '. $website. '<br>'; //unset the variable $website unset($website); //It will not display the name of the website echo 'After using unset() the domain name of website is : '. $website; ?>
输出:
Before using unset() the domain name of website is : javatpoint.com Notice: Undefined variable: website in C:xampphtdocsprogramunset.php on line 5 After using unset() the domain name of website is :
示例2:使用unset()
在此示例中,我们将使用unset()函数销毁变量。看下面的例子:
<?php
$var_value1 = 'test';
if (isset($var_value1)) {
//It will print because variable is set
echo "Variable before unset : ". $var_value1;
echo "</br>";
var_dump (isset($var_value1));
}
unset ($var_value1);
echo "</br>Variable after unset : ". $var_value1;
echo "</br>";
var_dump (isset($var_value1));
?>
输出:
Variable before unset : test bool(true) Notice: Undefined variable: var_value1 in C:xampphtdocsprogramunset.php on line 10 Variable after unset : bool(false)
示例3:未设置GLOBAL变量,但没有变化反映
如果您直接尝试取消设置函数内部的全局变量,则更改将在本地而不是全局反映。
<?php
$var_value1 = "Welcome to javatpoint";
// No changes will be reflected outside
function unset_var_value()
{
unset($var_value1);
}
unset_var_value();
echo $var_value1;
?>
输出:
Welcome to javatpoint.
示例4:当更改反映时未设置全局变量
使用$GLOBAL数组取消设置函数内部的全局变量。
<?php
$var_value1 = "Welcome to javatpoint";
// Changes will be reflected outside
function unset_var_value()
{
unset($GLOBALS['var_value1']);
}
unset_var_value();
echo $var_value1;
?>
输出:
Notice: Undefined variable: var_value1 in C:xampphtdocsprogramunset.php on line 11
示例5:销毁静态变量
<?php
function destroy_var()
{
static $var;
$var++;
echo "Value before unset: $var, ";
unset($var);
$var = 25;
echo "Value after unset: $var </br>";
}
destroy_var();
destroy_var();
destroy_var();
?>
输出:
Value before unset: 1, Value after unset: 25 Value before unset: 2, Value after unset: 25 Value before unset: 3, Value after unset: 25
类别:PHP 技巧、
本文收集自互联网,转载请注明来源。
如有侵权,请联系 wper_net@163.com 删除。

还没有任何评论,赶紧来占个楼吧!