php - PHP 更新文件中的值
问题描述
我有一个 PHP 脚本和一个文件。该文件仅包含“8”。我需要使用 PHP 脚本将此值更新为“9”(或任何值,只需将文件中的内容加 1)。
目前我有一个看起来像这样的文件:
<?php
file_put_contents("numberFile", file_get_contents("numberFile")[0]++);
?>
这应该将它从第一行 + 1 读取的内容写入文件。但是它不起作用。有一个更好的方法吗?
解决方案
$content = file_get_contents('numberFile');
if (isset($content))
{
$number = intval($content);
file_put_contents('numberFile', ++$number);
}
未经测试,但我怀疑这很接近你想要的东西,如果不完全一样的话。:)
正如评论中所述,不要试图在一行中完成所有操作,分解代码以便其可读。
推荐阅读
- mysql - SQL query return name field from tableA and name field from tableB match with id but tableB doesn't have name field
- c++ - Is there a cost difference between returning a function by reference vs passing the object through a void function?
- rust - How do I integrate a fork of one of the crates I'm using into my project?
- angular - Angular: how to resize Search Filter Pipe
- android - Implementation of library giving duplicate error
- ruby-on-rails - Authorization header is invalid -- one and only one ' ' (space) required in S3 File while accessing
- powerbi - 如何根据两列在 Power BI 中生成排名
- python - 计算大型复合形状中单位三角形的算法
- apache-atlas - 样条火花代理罐在后处理过程中出现错误
- rust - 接受字符串迭代器的函数的正确签名