【问题标题】:unable to understand work of unset() using indirect reference in PHP在 PHP 中使用间接引用无法理解 unset() 的工作
【发布时间】:2023-03-10 00:33:02
【问题描述】:

我是 PHP 新手,使用 5.6 版。在使用间接引用的情况下,我试图了解 unset() 的功能。 unset() 如果没有其他变量引用它的值,则设置前一个变量使用的内存空闲。在我的代码中,unset() 运行良好,它返回 false。但是当我使用间接引用变量时,它仍然返回 false。 这是我的代码...

  //unsetting the variable
  $unset_var="darsh";
        //Indirect refernces to variables
        $$unset_var="new to PHP";
        echo "\n\n".$darsh;
  unset($unset_var);
  if(isset($unset_var))
  {
        print "<br>".'$unset_var variable is not free to use';
  }
  else 
  {
        print "<br>".'$unset_var variable is free to use';
  }

【问题讨论】:

  • 然后呢?间接引用在哪里?

标签: php unset


【解决方案1】:

也许这会有所帮助:

$a = "x";
$$a = 'Value of $$a'; // creates a variable called $x
                      // because the literal value of $a = 'x'
                      // think about it as: ${$a}

echo $$a . "<br>";  // 'Value of $$a'
echo $x . "<br>";   // 'Value of $$a'

unset($a);

echo $a . "<br>";   // Notice: Undefined variable
echo $$a . "<br>";  // Notice: Undefined variable - because {$a} has been unset
echo $x . "<br>";   // 'Value of $$a' - $x has not been unset

【讨论】:

    猜你喜欢
    • 2013-11-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-21
    • 1970-01-01
    • 2019-02-14
    • 1970-01-01
    • 2022-06-12
    相关资源
    最近更新 更多