How do I change a static variables value in PHP?

后端 未结 4 809
天命终不由人
天命终不由人 2020-12-16 03:56

This is a simplified version of what I want to accomplish:

In my script I want a variable that changes true and false everytime the script is executed.



        
4条回答
  •  生来不讨喜
    2020-12-16 04:49

    PHP is not able to keep variable values between requests. This means that each time your script is called, the $bool-variable will be set to true. If you want to keep the value between requests you have to use sessions or, if you want the variable shared between sessions, some caching mechanism like APC or Memcache.

    Also, static is used in PHP to declare a variable shared on the class level. It is thus used in classes, and accessed like self::$variableName; or Foo::$variableName

    You can read more about static properties here. From the docs:

    Declaring class properties or methods as static makes them accessible without needing an instantiation of the class. A property declared as static can not be accessed with an instantiated class object (though a static method can).

    Also, note that the word static has been overloaded since PHP 5.3, and can also be used to denote Late Static Binding, by use of static::

提交回复
热议问题