PHP Increment a counting variable in a text file

前端 未结 5 1927
Happy的楠姐
Happy的楠姐 2020-12-10 20:14

This seems simple but I can\'t figure it out.

file_get_contents(\'count.txt\');
$variable_from_file++;
file_put_contents(\'count.txt\', $variable_from_file);         


        
5条回答
  •  误落风尘
    2020-12-10 20:47

    Exactly like you did it should work fine. Just capture the data from file_get_contents(), and check if both of those functions were successful.

    $var = file_get_contents('count.txt');
    if ($var === false) {
        die('Some error message.');
    }
    $var++;
    if (file_put_contents('count.txt', $var) === false) {
        die('Some error message.');
    }
    

提交回复
热议问题