unset variable in php

前端 未结 5 1797
执念已碎
执念已碎 2021-01-11 16:16

I just read about unset variable through php manual.

The php manual says \"unset() destroys the specified variables\"

This def seems per

5条回答
  •  执念已碎
    2021-01-11 16:23

    unset(self::$somethingstatic); will raise an Fatal error, because the variable is static (always there, can't be unset).

    the documentation refers specifically to static variables defined inside a function, consider:

    function t($stage)
    {
      static $shell = 23;
      switch($stage) {
        case 1:
          $shell++;
          break;
        case 2:
          unset($shell);
          break;
        case 3:
          $shell--;
        break;
      }
      echo $shell;
    }
    

    because $shell is a static variable, it's always there (static) so any other time you mention $shell that is simply a reference - when you unset it, you are unsetting the reference (think unlink'ing a symlink) - the static variable is however still there (that's what static means).

    thus if you call the above function t(1) will echo 24, t(2) will echo nothing, and t(3) will (rightly) echo 23 :)

    help any?

提交回复
热议问题