I am trying to create a hit counter that once it reaches a certain number (in this case, 5), it will no longer display the amount of hits. This is my code:
<?php
$count = ("hits.txt");
$hits = file($count);
$hits[0] ++;
$fp = fopen($count , "w");
fputs($fp , "$hits[0]");
fclose($fp);
if ($hits > 5) {
echo "More than 5 hits";
}
else {
echo $hits[0];
}
?>
What am I doing wrong?
You are overcomplicating things. It would be much easier to do it like this:
As for your current code, the problem is that you don’t test the number of hits with the correct syntax
$hits[0]— which you already use infputs— but with the wrong$hitsinstead. Remember that due to the wayfileworks,$hitsitself is an array. PHP will happily let you compare an array to an integer, and there are rules that define how the comparison works, but don’t go there.