静态变量在函数调用时不递增

时间:2022-06-25 20:25:15
function genTokenNo()
{
    static $i=0;
    $i=$i+1;
    return str_pad($i, 5, '0', STR_PAD_LEFT);
}

When I calls this function in other file the function doesn't returns unique value (an incremented value). Also while I echo this function in same file(where the function is made) it works fine. I know the concept of scope of static variable as I have already tried this by replacing $i by $_SESSION['i'] but no expected result. Thanks in advance.

当我在其他文件中调用这个函数时,函数不会返回唯一的值(一个递增的值)。同样,当我在同一个文件中(在创建函数的地方)回显这个函数时,它工作得很好。我知道静态变量范围的概念,因为我已经尝试过用$_SESSION替换$ I,但是没有预期的结果。提前谢谢。

5 个解决方案

#1


2  

Note the value of $i is not persisted. So whenever you run new request to the server, the script is reloaded and $i is reset to 0.

注意$i的值没有持久化。因此,无论何时向服务器运行新的请求,脚本都会被重新加载,$i将被重置为0。

To persist the variable in the per-user session use $SESSION['i'] but as you already tried, this will be unique per session, not globally.

要在每个用户会话中持久化变量,请使用$ session ['i'],但是正如您已经尝试过的,每个会话都是唯一的,而不是全局的。

To have globally unique number you need to store on the disk, using fopen/flock/fread/fwrite/fclose functions.

使用fopen/flock/fread/fwrite/fclose函数,您需要在磁盘上存储全局惟一的数字。

#2


0  

For a single request it should work fine -

对于单个请求,它应该工作得很好

function genTokenNo()
{
    static $i=0;    
    $i=$i+1;
    return str_pad($i, 5, '0', STR_PAD_LEFT);
}

echo genTokenNo() . ' - ' . genTokenNo() . ' - ' . genTokenNo();

Output

输出

00001 - 00002 - 00003

But if you are requesting multiple times then the value will be reset on every request and every time the response will be the same. I would suggest to store the value in database or some file. In this case using seesion woludn't be reliable.

但是如果您请求多次,那么每个请求的值都将重置,并且每次响应都是相同的。我建议将值存储在数据库或某个文件中。在这种情况下,使用seesion woludn是不可靠的。

#3


0  

Your structure will look like below:

您的结构将如下所示:

file1.php

file1.php

function genTokenNo()
{
    static $i=0;
    $i=$i+1;
    return str_pad($i, 5, '0', STR_PAD_LEFT);
}

file2.php

php

include('file1.php');
//^^^^^^^^^^^^^^^^^^^ Include file before call that function.

echo genTokenNo(); //00001
echo "<br />";
echo genTokenNo(); //00002
echo "<br />";
echo genTokenNo(); //00003
echo "<br />";
echo genTokenNo(); //00004

#4


0  

file1.php

file1.php

<?php
    function genTokenNo()
    {
        static $i=0;
        $i=$i+1;
        return str_pad($i, 5, '0', STR_PAD_LEFT);
    }
?>

file2.php

php

<?php
include('file1.php');
$sql = "INSERT INTO table SET TokenNo='".genTokenNo()."' ";
mysql_query($sql);
?>

#5


-1  

when function exists then lost the value of static variable

当函数存在时,则失去静态变量的值

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

#1


2  

Note the value of $i is not persisted. So whenever you run new request to the server, the script is reloaded and $i is reset to 0.

注意$i的值没有持久化。因此,无论何时向服务器运行新的请求,脚本都会被重新加载,$i将被重置为0。

To persist the variable in the per-user session use $SESSION['i'] but as you already tried, this will be unique per session, not globally.

要在每个用户会话中持久化变量,请使用$ session ['i'],但是正如您已经尝试过的,每个会话都是唯一的,而不是全局的。

To have globally unique number you need to store on the disk, using fopen/flock/fread/fwrite/fclose functions.

使用fopen/flock/fread/fwrite/fclose函数,您需要在磁盘上存储全局惟一的数字。

#2


0  

For a single request it should work fine -

对于单个请求,它应该工作得很好

function genTokenNo()
{
    static $i=0;    
    $i=$i+1;
    return str_pad($i, 5, '0', STR_PAD_LEFT);
}

echo genTokenNo() . ' - ' . genTokenNo() . ' - ' . genTokenNo();

Output

输出

00001 - 00002 - 00003

But if you are requesting multiple times then the value will be reset on every request and every time the response will be the same. I would suggest to store the value in database or some file. In this case using seesion woludn't be reliable.

但是如果您请求多次,那么每个请求的值都将重置,并且每次响应都是相同的。我建议将值存储在数据库或某个文件中。在这种情况下,使用seesion woludn是不可靠的。

#3


0  

Your structure will look like below:

您的结构将如下所示:

file1.php

file1.php

function genTokenNo()
{
    static $i=0;
    $i=$i+1;
    return str_pad($i, 5, '0', STR_PAD_LEFT);
}

file2.php

php

include('file1.php');
//^^^^^^^^^^^^^^^^^^^ Include file before call that function.

echo genTokenNo(); //00001
echo "<br />";
echo genTokenNo(); //00002
echo "<br />";
echo genTokenNo(); //00003
echo "<br />";
echo genTokenNo(); //00004

#4


0  

file1.php

file1.php

<?php
    function genTokenNo()
    {
        static $i=0;
        $i=$i+1;
        return str_pad($i, 5, '0', STR_PAD_LEFT);
    }
?>

file2.php

php

<?php
include('file1.php');
$sql = "INSERT INTO table SET TokenNo='".genTokenNo()."' ";
mysql_query($sql);
?>

#5


-1  

when function exists then lost the value of static variable

当函数存在时,则失去静态变量的值

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