static Keyword in PHP

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

---------------------- ------

Then, each time the function is called, that variable will still have the information it contained from the last time the function was called.

1 comments:

Click here for comments
Anonymous
April 12, 2022 at 4:10 PM ×

Maaf link hidup tidak akan tampil di sini!

Selamat Anonymous dapat PERTAMAX...! Silahkan antri di pom terdekat heheheh...
Balas
avatar
admin

Show Konversi KodeHide Konversi Kode Show EmoticonHide Emoticon

:)
:(
=(
^_^
:D
=D
=)D
|o|
@@,
;)
:-bd
:-d
:p
:ng
:lv
Thanks for your comment