Hmm,
What I would do i rearange the code.
if (file_exists("numberOfHits.txt"))
{
$hit = file_get_contents("numberOfHits.txt");
$hit = (int)$hit;
$hit = $hit + 1;
$hitS = (str)$hit;
file_put_contents("numberOfHits.txt, $hitS");
echo "This page has been visited $hit times.";
}
else {
//Hit file cannot be found;
$hit = 1;
file_put_contents("numberOfHits.txt",$hit);
echo "An error occured in the hit counter.";
}
Probably not exact (More than likely no way near correct) but if the syntax was 100% I think that would work fine but I would recommend using the fopen(), fread(), and fwrite() functions.
This is a counter I have used in the past...
$filename= "hit_counter.txt" ;
$fd = fopen ($filename , "r") or die ("Can't open $filename") ;
$fstring = fread ($fd , filesize ($filename)) ;
echo "$fstring" ;
fclose($fd) ;
$fd1 = fopen ($filename , "w") or die ("Can't open $filename") ;
$fcounted = $fstring + 1 ;
$fout= fwrite ($fd , $fcounted ) ;
fclose($fd1) ;