Why, after there will be a coincidence, then the value of the session is minus only 1 time?

<?php error_reporting(-1); header('Content-Type: text/html; char set=utf-8'); ini_set('display_errors', 'On'); session_start(); $p1 = $_SESSION['player1'] = 10; //человек $comp = $_SESSION['computer'] = 10; //комп echo '$_SESSION["player1"] = ' . $p1 . '<br>'; if(isset($_GET['num'])){ if($_GET['num'] == rand(1, 3)){ $p1--; echo 'Совпало ' . $p1; } else{ echo 'Не совпало'; } } ?> <form action="" method="get"> Введите число от 1 до 3 <input type="number" name="num"> <input type="submit" name="Отправить"> </form 

    2 answers 2

    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> 
    • And how to do that the value of 10 is not overwritten? - DivMan

    Because each time in the session the values ​​of player1 and computer overwritten to the value 10 , and then the value of the variable changes, not the session value. It should work like this:

     <?php error_reporting(-1); header('Content-Type: text/html; char set=utf-8'); ini_set('display_errors', 'On'); session_start(); $_SESSION['player1'] = isset($_SESSION['player1']) ? $_SESSION['player1'] : 10; //человек $_SESSION['computer'] = isset($_SESSION['computer']) ? $_SESSION['computer']: 10; //комп echo '$_SESSION["player1"] = ' . $_SESSION['player1'] . '<br>'; if(isset($_GET['num'])){ if($_GET['num'] == rand(1, 3)){ $_SESSION['player1']--; echo 'Совпало ' . $_SESSION['player1']; } else{ echo 'Не совпало'; } } ?> <form action="" method="get"> Введите число от 1 до 3 <input type="number" name="num"> <input type="submit" name="Отправить"> </form> 
    • And how to do that the value of 10 is not overwritten? - DivMan
    • I need to initially be 10, and your code immediately gives out 1 - DivMan
    • Wrong, corrected the answer - ilyaplot
    • all the same 1 remained - DivMan
    • Because in session 1 recorded. Should decrease. Before checking the script, clear the session. - ilyaplot