问题描述
- php的计数器每次都会清零
-
<?php
$count_num=0;
if(file_exists("counter.txt"))
{
$fp=fopen("counter.txt","r");
$counter_num=fgets($fp,9);//读取前八位数字
$count_num++;
fclose($fp);}
$fp=fopen("counter.txt","w");
fputs($fp,$count_num);
fclose($fp);echo" hahaha $count_num hahah";
?>这个每次显示的都是1 就算人工修改了txt也不行 php初学者 请问问题出在哪里?谢谢!!
解决方案
$count_num 毫无意义
可以去掉
$counter_num=0;
if(file_exists("counter.txt"))//true exist
{
$fp=fopen("counter.txt","r");
$counter_num=fgets($fp,9);//取数
$counter_num++; //加数
fclose($fp);
}
$fp=fopen("counter.txt","w");
fputs($fp,$counter_num); //存数
fclose($fp);
echo" hahaha $counter_num hahah";
解决方案二:
变量名不一样
$counter_num=fgets($fp,9); // ==》 $counter_num
其它地方都是$count_num
时间: 2024-09-14 16:05:23