PHP函数,每次执行脚本时将变量增加1 [英] PHP function to increment variable by 1 each time script is executed

查看:530
本文介绍了PHP函数,每次执行脚本时将变量增加1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每次执行脚本时,都可以将变量或数组增加1。

Is it possible to increment a variable or array by 1 each time the script is executed.

例如,执行脚本并设置变量到 $ increment ='1';

An example would be, I execute the script and it sets a variable to $increment = '1';

然后我关闭脚本,30分钟后回来执行

I then close the script and come back 30 min later to execute the script again.

这次变量需要 $ increment ='2';

脚本将每30分钟从cron运行总共8次。

The script will be run from cron every 30 minutes for a total of 8 times.

第8次执行后,变量将需要要回退到 $ increment ='1';

After the 8th execution the variable would need to be rewound back to $increment = '1';

这是否可以通过会话?是否可以为cli sapi设置会话?

Is this possible through session? Can session be setup for the cli sapi ?

或者更容易输出带有下一个增量编号的文本文件,只需从文本文件中取出值?

Or would it be easier to output a text file with the next increment number and just pull the value from the text file?

所有的帮助是值得赞赏的。

All help is appreciated.

推荐答案

变量的值到脚本调用之间的非易失性源。此外,你必须考虑种族条件,如果(通常情况下)脚本几乎可以在同一时间调用。

You will have to persist the value of the variable to a non-volatile source between calls to the script. Furthermore, you'll have to consider race conditions if (as is often the case) the script can be called at almost the same time.

是将您的变量存储在维护事务安全的数据库中。即使是最便宜的webhosters提供某种形式的数据库,将满足这个要求...具体取决于您的特定设置。

The best bet for this, is to store your variable in a database that maintains transactional safety. Even most cheap webhosters offer some form of database that will meet this requirement ... the specifics depend on your particular setup.

这篇关于PHP函数,每次执行脚本时将变量增加1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆