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:
Example
<!DOCTYPE html>
<html>
<body>
<?php
function myTest() {
static $x = 0;
echo $x;
$x++;
}
myTest();
echo "<br>";
myTest();
echo "<br>";
myTest();
?>
</body>
</html>
-------------------------------
OUTPUT
0
1
2
---------------------- ------
Then, each time the function is called, that variable will still have the
information it contained from the last time the function was called.
Related Post
Variables in PHP
Variables are "containers" for storing information.
Creating (Declaring) PHP Variables
In PHP, a
PHP DB Connection Code
$servername = "localhost";$database= "db_form";$username = "root";$password = "";// Create connect
static Keyword in PHP
Normally, when a function is completed/executed, all of its variables are deleted.
However, somet
Gglobal Keyword in PHP
The global keyword is used to access a global variable from within a function.
To do this, use th
1 comments:
Click here for commentsMaaf link hidup tidak akan tampil di sini!
Show Konversi KodeHide Konversi Kode Show EmoticonHide Emoticon