9

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.

<?php
    static $bool = true;

    // Print differente messages depending on $bool
    if( $bool == true )
        echo "It's true!";
    else
        echo "It's false!";

    // Change $bools value
    if( $bool == true )
        $bool = false
    else
        $bool = true;
?>

But obviously what I'm doing is wrong. The variable $bool is constantly true and I haven't fully grasped the concept of static variables I presume. What am I doing wrong?

Weblurk
  • 6,562
  • 18
  • 64
  • 120

4 Answers4

18

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::

Jeff Puckett
  • 37,464
  • 17
  • 118
  • 167
PatrikAkerstrand
  • 45,315
  • 11
  • 79
  • 94
  • Hmm.. So if I moved the static variable to a class, would I then be able to switch it's value back and forth between the executions of my main script? – Weblurk Feb 29 '12 at 10:19
  • 4
    No. Static can be used to declare class variables or within function to declare a variable which persists over function calls, but not over executions of the script. – Claudio Albertin Feb 29 '12 at 10:22
2

A static value will not persist over executions. Every time the script is executed $bool is initialized. I think you should persist this value in a file to keep it simple.

addex03
  • 197
  • 1
  • 2
  • 12
  • I thought that was the point with static variables, that they DO persist over executions. I'd prefer not to keep it in a separate file or is that the only solution? – Weblurk Feb 29 '12 at 10:17
  • You can use Memcache, APC, Redis, a file... whatever you want. But you have to store the current value of the variable somewhere. – Claudio Albertin Feb 29 '12 at 10:24
2

I think you need to better understand the point of a static variable. The storage for the variable is allocated (and deallocated) on the call stack, so from a software engineering point of view, its value cannot be changed in run time.

There are better solutions as suggested above for this.

BenM
  • 52,573
  • 26
  • 113
  • 168
  • 3
    Actually, static variables in PHP are not static at all.. their values can be changed during execution. It's just a shared variable of a class. – PatrikAkerstrand Feb 29 '12 at 10:23
  • This enables code like `class Lookup { protected static $_cache = array(); public function expensiveLookup ($x) { if(!isset(self::$_cache[$x]) {self::$_cache[$x] = $this->_lookup($x);} return self::$_cache[$x]; }}` – PatrikAkerstrand Feb 29 '12 at 10:25
-1

It's an easy thing to use static keyword in php. Here I've used a static variable and a static method. Just try this out.

<?php
   class Test{
        protected static $myVar;
        public static function printHello(){         
            self::$myVar = 'Hello'; //This will assign the value to the static variable $myVar
            echo self::$myVar;  //This prints the value of static variable $myVar
        }
    }
    Test::printHello();
?>
bairavand
  • 335
  • 3
  • 11