如果我想在文本文件中存储和增加玩家的分数。每轮生成不同的文本文件。这是代码:
$string = '';
for ($i = 0; $i < 20; $i++) {
$d = rand(1, 30) % 2;
$char = $d ? chr(rand(65, 90)) : chr(rand(48, 57));
$string .= $char;
}
$ourFileName = $string . ".txt";
$counter = file($ourFileName);
echo $counter;
if (strcasecmp($answer3, $answer4) == 0) {
$counter[0]++; // increase the counter by one
echo "<h3 style='color:White; margin-left:600px;'> Player 2 score: $counter[0] </h3>";
echo " <h3 style='color:White; margin-left:600px;'>Well done, the correct answer is : $answer3 </h3>";
} else {
$counter[0]--; // increase the counter by one
echo "<h3 style='color:White; margin-left:600px;'> Player 2 score: $counter[0] </h3>";
echo "<h3 style='color:White; margin-left:600px;'> The correct answer is : $answer3 </h3>";
}
$fp = fopen($ourFileName, "w");
fwrite($fp, $counter[0]);
fclose($fp);
此代码生成一个包含随机字符串的文件,但不会增加分数并呈现null。如果我创建一个具有特定名称的文件,它可以工作,但我希望文件具有不同的名称。我的代码有什么问题???