In php, how can I make sure that static variables declared in a function will not be re-triggered whenever a function is called?

It seems that the static variable declared in the function is re-triggered whenever the function is called, how can I use this function so that re-calling the function reuses the static parameter?

I defined the testStatic function in static.php

here is static.php:

<?php
function testStatic()
{
    static $staticV = 0;
    echo $staticV;
    $staticV;
}
?>

I call "testStatic" from index.php

here index.php:

<?php include "./static.php";?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"   "http://www.w3c.org/TR/html4/strict.dtd">
<?php
testStatic();
?>

<html> 
.
.
.
<html>

index.php , testStatic '0', index.php. testStatic '0'. , 'staticV' 'testStatic' , index.php.

. index.php

+3
5

, PHP script, . HTTP- script .

- index.php PHP, $staticV 0.

, - index.php, .

$staticV -, . Sessions .

+7

HTTP - , - . , PHP index.php, index.php - , index.php, .

. , , .. , , . , .

,

<?php
session_start();

if(isset($_SESSION['staticV']))
    $_SESSION['staticV']++;
else
    $_SESSION['staticV'] = 0;

echo $_SESSION['staticV'];
?>
+4

. ​​(!) script. script, php- script...

cookie ( php) .

+3

hmmmm php . , php- script, reset. , , . / cookie .

+1

, , ( , , $_SESSION), reset. , . , , , , . , . DB, , ... , .

Remember that if you want this number to be in a sequence not for a specific user, but for all users you will have to do additional work to handle synchronization.

+1
source

Source: https://habr.com/ru/post/1713789/


All Articles