php unset

php里面的unset 可以把值置空,但是并不会改变值已经有的索引位置

 1 $testArr=array('frist','second','third');
 2 echo '<pre>';
 3 echo '<br/>---------------------------------proto ------------------------------</br>';
 4 print_r($testArr);
 5 foreach($testArr as $key=>$val){
 6     unset($testArr[$key]);
 7 }
 8 echo '<br/>---------------------opo---------------------------</br>';
 9 print_r($testArr);
10 $testArr[]='good!';
11 echo '<br/>---------------------add---------------------------</br>';
12 print_r($testArr);
13 echo '</pre>';

If a static variable is unset() inside of a function, unset() destroys the variable only in the context of the rest of a function. Following calls will restore the previous value of a variable.

 1 function foo()
 2 {
 3     static $bar;
 4     $bar++;
 5     echo "Before unset: $bar, ";
 6     unset($bar);
 7     $bar = 23;
 8     echo "after unset: $bar<br/>";
 9 }
10 
11 foo();
12 foo();
13 foo();
14 


vld分析: 15 function foos(&$bar) 16 { 17 unset($bar); 18 $bar = "blah"; 19 } 20 21 $bar = 'something'; 22 echo "$bar<br/>"; 23 24 foos($bar); 25 echo "$bar<br/>";



If a variable that is PASSED BY REFERENCE is unset() inside of a function, only the local variable is destroyed. The variable in the calling environment will retain the same value as before unset() was called.

这是手册中发现的,突然感觉把手册再翻一遍,是个正确的选择

原文地址:https://www.cnblogs.com/zhangjun516/p/3088624.html