Actually the question is how to add to the variable +1 each time you start it? My example always shows $ i = 1, but I don’t know how to make $ i increase by 1.

<? $i = 0; if ($i < 2) { echo 'Исход 1'; } if ($i > 2 && $i < 6) { echo 'Исход 2'; } if ($i > 6 && $i < 12) { echo 'Исход 3'; } $i++; ?> 
  • While it occurred to write $ i ++ to a file and replace $ i = 0; on file_get_contents (); - user5419467
  • You can write to a file, to any cache, to any table with statistics in the database - DanielOlivo
  • You can read only by writing to a file or database - Sergey Omelchenko

2 answers 2

Value can be saved in PHP session

 $i = $_SESSION['count']; if ($i < 2) { echo 'Исход 1'; } if ($i > 2 && $i < 6) { echo 'Исход 2'; } if ($i > 6 && $i < 12) { echo 'Исход 3'; } $i++; $_SESSION['count'] = $i; 

    Save value to file.

    data/ - directory path, you need to make the interpreter read / write.

     $fp = fopen("data/counter.txt", "r+"); if(!$fp)$fp=fopen("data/counter.txt", "x+"); if(!$fp)$fp=fopen("data/counter.txt", "r+"); flock ($fp,LOCK_EX); $counter= fgets($fp, 10)+1; fputs($fp, $counter); flock ($fp,LOCK_UN); fclose($fp); echo $counter; 

    flock and the third fopen - in case of a parallel launch of the script.