What why? The script runs once. You click the Submit button. It runs once. And you have written that if the values match, then you need to subtract one once. What did you expect at all? In addition, the script runs every time from the beginning. And there will always be 10 in the beginning.
And it can be combined
if(isset($_GET['num']) and $_GET['num'] == rand(1, 3)){ $p1--; echo 'Совпало ' . $p1; } else { echo 'Не совпало'; }
The simplest thing is to check at the beginning - does the session exist? If not, then write 10. Otherwise, do not write, and take what is, until it becomes less than zero. If less than zero, then we start everything from the very beginning.
if (!isset($_SESSION['player1'])) { $_SESSION['player1']=10; $p1=$_SESSION['player1']; } elseif ($_SESSION['player1']<0) { echo ('Меньшя нуля'); unset($_SESSION['player1']); //удалем }
Eventually:
<?php header('Content-Type: text/html; char set=utf-8'); ini_set('display_errors', 'On'); session_start(); if (!isset($_SESSION['player1'])) { $_SESSION['player1']=10; } elseif ($_SESSION['player1']<0) { echo ('Меньшя нуля'); unset($_SESSION['player1']); //удалем } echo '$_SESSION["player1"] = '.$_SESSION['player1'].'<br>'; if(isset($_GET['num']) and $_GET['num'] == rand(1, 3)){ $_SESSION['player1']--; echo 'Совпало '.$_SESSION['player1']; } else { echo 'Не совпало'; } ?> <form action="" method="get" name='formrand'> <label>Введите число от 1 до 3 <input type="number" name="num" value='1'></label> <input type="submit" name="Отправить"> </form>