Increment the scores in different text files
If I want to store and increment the score of the players in a text file. Every round generates different text file. Here is the code:
$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 :开发者_如何学Go $answer3 </h3>";
}
$fp = fopen($ourFileName, "w");
fwrite($fp, $counter[0]);
fclose($fp);
this code generates a file with random string but it doesn't increment the score and renders null. If I create a file with a specific name it works but I want the files to be with different names. Is there any problem in my code???
精彩评论