如何将txt文件中的所有数字相加并制作成一个有值的变量?

How to add all numbers in txt file and make into a variable with a value?

基本上,我试图让它在按下按钮时将 1 登录到 data.txt,然后 PHP 将累加 1的,并在页面上显示该号码供所有用户查看。我正在尝试完整地执行此操作 PHP,但如果需要,我可以使用 javascript 和 HTML。这是我的完整代码:

<?php

$newline = "\n";
$filename = 'data.txt';
$somecontent = ("1");


if (is_writable($filename)) {



    if (!$handle = fopen($filename, 'a')) {
         echo "Cannot open file ($filename)";
         exit;
    }

    if (fwrite($handle, "$somecontent $newline") === FALSE) {
        echo "Cannot write to file ($filename)";
        exit;
    }


    fclose($handle);

} else {
    echo "The file $filename is not writable";
}
?>
<html>
    <head>
    <title>__Work-In-Progress__</title>
    </head>
    <form action = "<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method = post>
    <input type = "submit" name = "button" value = "Submit" >
</form>
</html>

它有点乱,但是啊,它完成了工作。提前致谢!

编辑:网站在这里,但我还没有完成 CSS 和所有内容。>> http://clickthebutton.rf.gd/index.php

是的,我明白了!这是我的完整代码:

<?php

$newline = "\n";
$filename = 'data.txt';
$somecontent = ("1");


if (is_writable($filename)) {



    if (!$handle = fopen($filename, 'a')) {
         echo "Cannot open file ($filename)";
         exit;
    }

    if (fwrite($handle, "$somecontent $newline") === FALSE) {
        echo "Cannot write to file ($filename)";
        exit;
    }


    fclose($handle);

} else {
    echo "The file $filename is not writable";
}

$no_of_lines = count(file($filename)); 
 
echo "The button has been pressed $no_of_lines times."

?>
<html>
    <head>
    <title>__Work-In-Progress__</title>
    </head>
    <form action = "<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method = post>
    <input type = "submit" name = "button" value = "Submit" >
</form>
</html>