tags:

views:

40

answers:

1

The value of a local variable of a function has to be retained over multiple calls to the function. How should that variable be declared in PHP?

+11  A: 

using static. example:

function staticDemo() {
  static $x =1;
  echo $x;
  $x++;
}

the variable $x gets that static value the first time the function is called, afterwards it retains it's state.

example:

staticDemo(); // prints 1
staticDemo(); // prints 2
staticDemo(); // prints 3
GSto