Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
PHP The static Keyword
#1
Thumbs Up 
Normally, when a function is completed/executed, all of its variables are deleted. However, sometimes we want a local variable NOT to be deleted. We need it for a further job.

To do this, use the static keyword when you first declare the variable:
<?php

function myTest() {

    static $x = 0;

    echo $x;

    $x++;

}


myTest();

myTest();

myTest();

?>

Then, each time the function is called, that variable will still have the information it contained from the last time the function was called.
[b]Note:[/b] The variable is still local to the function.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)