2017-08-03 163 views
2

我正在使用HTML和PHP進行10輪的Rock,Paper和Scissors遊戲。我遇到的唯一問題是$player,$comp$round不會增加,並且始終會達到初始值。可能是什麼問題?PHP:變量不增加

這是我的PHP代碼部分:

<?php session_start(); 

if(!isset($_SESSION['use'])) 
{ 
    header("Location:enter.php"); 
} 

if (isset($_GET['logout'])) { 
    session_destroy(); 
    unset($_SESSION['username']); 
    header("location: enter.php"); 
} 

$Computer='none'; 
$user_choice=''; 
$judgelose = array(); 
$judgewin = array(); 
$judgedraw = array(); 

$round=1; 
$player=0; 
$comp=0; 

if(isset ($_POST['choice'])) 
{ 

    $Choosefrom= array('Rock', 'Paper', 'Scissors'); 
    $Choice= rand(0,2); 
    $Computer=$Choosefrom[$Choice]; 
    $user_choice=$_POST['choice']; 

    if($user_choice == $Computer){ 
    array_push($judgedraw, 'Result : Draw'); 
    } 
    else if($user_choice == 'Rock' && $Computer == 'Scissors' || $user_choice == 'Scissors' && $Computer == 'Paper' || $user_choice == 'Paper' && $Computer == 'Rock'){ 
    array_push($judgewin, 'Result : Win'); 
    $player++; 
    } 
    else if($user_choice == 'Rock' && $Computer == 'Paper' || $user_choice == 'Scissors' && $Computer == 'Rock' || $user_choice == 'Paper' && $Computer == 'Scissors'){ 
    array_push($judgelose, 'Result : Lose'); 
    $comp++; 
    } 
} 
?> 
+3

您需要將這些值存儲在會話中,否則每次加載頁面時都會將其重置。 –

+0

喬恩是對的,你應該只是讓你的評論的答案... – Theyouthis

回答

0

您需要將這些值存儲在會話以及否則每次加載它們將被重置的頁面。 PHP不會記住頁面加載之間的值。這是會議的目的。您應該檢查該值是否存在於會話中並獲取它。如果它不存在,請設置一個初始默認值。然後,對變量進行許多任何更改並最終將其保存回會話中。例如:

//check if the session for somevalue exists 
if(isset($_SESSION['somevalue'])){ 
    //get it 
    $somevalue = $_SESSION['somevalue']; 
} else { 
    //set a default value if not isset 
    $somevalue = 0; 
} 

//do something with somevalue 
$somevalue++; 
//continue doing something with somevalue 

//at bottom, save somevalue back into session. 
$_SESSION['somevalue'] = $somevalue;