我真的是 php 新手。我决定根据我看过的脚本制作一个计数器。我已经对其进行了更改。我正在尝试弄清楚如何重置计数器。
$userCount = file_get_contents("count.txt");
$userCount = trim($userCount);
$userCount = $userCount + 1;
$countReset = $userCount - $userCount;
$file = fopen("count.txt","w+");
fwrite($file,$userCount);
fclose($file);
print "The number of visitors is: $userCount";
if ($userCount < 20){
echo 'not yet';
}
else {
echo 'done!';
}
if ($userCount > 40){
fwrite($file,$countReset);
fclose($file);
}
我尝试从自身中减去计数器以使其回到 0。
$countReset = $userCount - $userCount;
不过,好像不行。计数器本身可以工作,但我无法将其重置为 0。
这只是我作为一种学习 php 的方式所做的类似脚本的印象。另外,对格式感到抱歉,与这个帖子编辑器斗争。
对脚本有任何帮助吗?
最佳答案
您在尝试再次写入之前关闭了文件。在你的第二个 fwrite
之前,添加:
$file = fopen("count.txt","w+");
关于php - 需要帮助重置 php 计数器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9316298/