Читать книгу PHP Programming for Beginners. Key Programming Concepts. How to use PHP with MySQL and Oracle databases (MySqli, PDO) - Sergey D Skudaev - Страница 8
Static variable
ОглавлениеA static variable is the same as a local variable, but it is not destroyed when the function execution is ended.
<?php
function greetme ($name) {
static $greeting=“Hi There”;
$greeting=$greeting.””. $name;
echo $greeting.”! <br>”;
}
greetme (“John’);
greetme (“John’);
greetme (“John’);
?>
Output:
Hi There John!
Hi There John John!
Hi There John John John!
The value of the static $greeting variable is changing after each function call because the $name variable is added to it each time.
Remove the static keyword and the Output: of the function will be the same in the second and the third call
<?php
function greetme ($name) {
$greeting=“Hi There”;
$greeting=$greeting.””. $name;
echo $greeting.”! <br>”;
}
greetme (“John’);
greetme (“John’);
greetme (“John’);
?>
Output:
Hi There John!
Hi There John!
Hi There John!