在总和中添加一个字符串和一个数字 [英] add a string and a number in a sum

查看:62
本文介绍了在总和中添加一个字符串和一个数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够在每次重新加载页面时+1到$i. 我遇到了一个非常简单的问题,我正在努力寻找在线解决方案.

I want to be able to +1 to $i every page reload. I have come across a very simple issue, that I am struggling to find a solution online.

这里是我的代码:

$backupNumber = fopen("$v", "r+") or die("Unable to open file!");
$i = fread($backupNumber,filesize("invoices/invoice1/backupN.txt"));

$i = intval($i);
$i = $i + 1;
echo $i;
fwrite($backupNumber,$i);
$a = "invoices/" . $invoiceN . "/backup" . $i;
fclose($backupNumber);

和txt文件中只是数字"1"开头.

and in the txt file is simply the number '1' to start off with.

当我回显$ i输出页面时,重新加载页面时会出现问题:
2然后13然后1214然后12131215然后2147483648等.
我希望它是简单的输出
2再3再4等

The issue occurs when reloading the page when I echo $i it outputs:
2 then 13 then 1214 then 12131215 then 2147483648 etc.
I want it to simple output
2 then 3 then 4 etc

推荐答案

您追加了文本文件,这就是这种情况的原因.

You append the text file, that is why this is happening.

我的建议是使用 file_get_contents file_put_contents .

$i = file_get_contents("invoices/invoice1/backupN.txt");
$i++;
Echo $i;
file_put_contents("invoices/invoice1/backupN.txt" $i);

文件获取和放置内容始终读取整个文本文件.
我认为您不需要嵌入字符串,没有它就可以工作.

File get and put contents always reads the whole text file.
I don't think you need to intcast the string, it should work without it.

代码也可以是一个衬里.杂乱但紧凑.

The code can be a one liner too. It's messy but compact.

file_put_contents("invoices/invoice1/backupN.txt", file_get_contents("invoices/invoice1/backupN.txt")+1);

这篇关于在总和中添加一个字符串和一个数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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