2016-09-22 92 views
-1

页面重新加载或提交它应该添加猜测的单词到$ guesses并添加一个管道。每次重新加载它应该不添加替换。我将它存储在一个隐藏的元素,以保持页面加载,但它似乎并没有工作。我究竟做错了什么?为什么不会变量连接?

<html> 
<body> 
    <p style='text-align:center;'> 
    <?php 
     $wordChoices = array("grape","apple","orange","banana","plum","grapefruit");//Course provided aray/code 
     $randChoice; 
     $tempVar; 
     if(isset($_POST['gu'])) 
      $guesses=$_POST['gu']; 
     else 
      $guesses=""; 
     echo "|||||||||" . $guesses . "||||||||"; 
     if(empty($_POST['va'])) 
     { 
      $randChoice=rand(0,5); 
     } 
     else 
     { 
      $randChoice=$_POST['va'];  
     } 

     //$randChoice = rand(0,5);//NEEDS FIX 
     for($i=0;$i<=5;$i++)//output of array and green text 
     { 
      if($i==$randChoice) 
       echo "<span style='color: green; text-align:center;'>$wordChoices[$i]</span>";//prints green word 
      else 
       echo "<span style='text-align:center;'>$wordChoices[$i]</span>";//print word 
      if($i<5)//kills extrta "|" 
       echo " | ";//print break between words 

     } 
    ?> 
    </p> 
    <h1 style='text-align:center;'>Word guess</h1> 
    <div style='text-align:center'> 
     <a href='lab1.php'>Refresh This Page</a> 
    </div> 
    <h3 style='text-align:center;'>Guess the word I'm thinking</h3> 
    <form action="<?php $_SERVER['PHP_SELF'];?>" method='POST' style='text-align:center;'> 
     <input type='text' name='tb1' placeholder=<?php echo $wordChoices[$randChoice];?>> 
     <input type='submit' name='butt'> 
     <input type='hidden' name='va' id='va' value="<?php echo $randChoice;?>"> 
     <input type='hidden' name='gu' id='gu' value="<?php echo $guesses;?>"> 
    </form> 
    <?php 
     if($_POST) 
     { 
      if(!empty($_POST['tb1'])) 
      { 
       $guesses += $_POST['tb1'] . "|"; 
      } 
      if(isset($_POST['tb1']) && $_POST['tb1'] == $wordChoices[$randChoice])//checks to see if correct 
       echo "<p style='text-align:center; color:red;'>You guessed " . $wordChoices[$randChoice]. " and that's CORRECT!!! (3)</p>";//prints out correct 
      else if(!isset($_POST['tb1'])||$_POST['tb1']=="")//checks if nothing entered 
       echo "<p style='text-align:center; color:red;'>Come on, enter something (2)</p>";//prints out message to enter text 
      else if(isset($_POST['tb1']) && isValid($_POST['tb1']) && strtoupper($_POST['tb1'])!=strtoupper($wordChoices[$randChoice]))//checks for valid guess but incorrect term 
       echo "<p style='text-align:center; color:red;'>Sorry " . $_POST['tb1'] . " is wrong. Try again (4)</p>";//prints out valid incorrect guess 
      else if(isset($_POST['tb1']) && !isValid($_POST['tb1']))//checks if guess is valid 
       echo "<p style='text-align:center; color:red;'>Hey, that's not even a valid guess. Try again (5).</p>";//prints out invalid 
      echo "<ul>".$guesses."</ul>"; 
     } 
     else 
      echo "<p style='text-align:center; color:red;'>It's time to play the guessing game! (1)</p>"; 

     function isValid($textResp)//checks is guessed word is a valid word 
     { 
      print($textResp); 
      $wordChoices = array("grape","apple","orange","banana","plum","grapefruit"); 
      if($textResp==$wordChoices[0]) 
       return true; 
      else if($textResp==$wordChoices[1]) 
       return true; 
      else if($textResp==$wordChoices[2]) 
       return true;      
      else if($textResp==$wordChoices[3])     
       return true; 
      else if($textResp==$wordChoices[4]) 
       return true; 
      else if($textResp==$wordChoices[5]) 
       return true; 
      else 
       return false; 

     } 
    ?> 
</body> 

回答

1

Unsimilar到JavaC#或类似,运营商+-*只能在数字工作。字符串具有用于字符串串联的.运算符。如果您在字符串上使用+,PHP将键入一个字符串到数字。底线使用..=