Lecture
In addition to local and global variables, there is another type of variable in PHP - static variables .
If the body of the user function is declared static variable , the compiler will not delete it after the completion of this function.
Static variable declaration:
<?php
function funct(){
static $int = 0; // верно
static $int = 1+2; // неверно (поскольку это выражение)
static $int = sqrt(121); // неверно (поскольку это тоже выражение)
$int++;
echo $int;
}
?>
An example of a user function containing static variables:
<? php
function funct ()
{
static $ a ;
$ a ++;
echo "$a" ;
}
for ($ i = 0 ; $ i ++< 10 ;) funct ();
?>
This script displays the line:
1 2 3 4 5 6 7 8 9 10
If we remove the static statement, the line will be displayed:
1 1 1 1 1 1 1 1 1 1
This is due to the fact that the $ a variable will be deleted at the end of the function and will be reset every time it is called. The variable $ a is incremented immediately after zeroing, and only then displayed.
See also:
Local Variables in PHP
Global variables in php
Comments
To leave a comment
Running server side scripts using PHP as an example (LAMP)
Terms: Running server side scripts using PHP as an example (LAMP)