其他意外 (T_ELSE)

unexpected else (T_ELSE)

这是我当前的代码:

时没有自毁
if(!$_SESSION['answer']){$_SESSION['answer'] = rand(0,$_POST['amount']);}

if(!$_SESSION['answer']){$_SESSION['answer'] = rand(0,$_POST['amount']);

我不完全知道自己在做什么,也不了解编程术语,我对所有这些都是新手,如果您能找出这个很可能非常简单但很简单的语法错误,我将不胜感激我只是看不到它。

编辑:请注意,我不希望您更正任何其他代码,但语法错误 TY!

<? session_start(); ?>
<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title> Jelly bean Game </title>
</head>
<body>
<?php

  if(!$_POST["submit"]){
    SESSION_DESTROY();
?>


<h2> Jelly Bean game! <h2>

<form action="<?=$_SERVER['php_self']?>" method="post">
  <input type="text" name="amount" placeholder="Add your jellybeans"/>
  <input type="submit" name="submit" value="add">
</form>

<?
}else{
$amount = $_POST['amount']; //variable
$guess = $_POST['guess']; //variable
$gz = ""; // empty variable

if(!$_SESSION['amount']){$_SESSION['amount'] =$amount;}
if(!$_SESSION['answer']){$_SESSION['answer'] = rand(0,$_POST['amount']);}
if(!ctype_digit($amount)){$error[] = "please do not use letters dots or     symbols when submitting the amount of beans you want to use!";}
if($error){
  echo "<div class=\"errorbox\">";
foreach($error as $val){
  submitAgainForm();
  echo "<p>" . $val . "</p>";
}
echo "</div>";


    }elseif($_POST['guess'] == ""){
        echo "Take a guess!";
        echo $amount;
        echo $answer . "123";
        guessAgainForm();
    }else{
      if($_POST['guess'] > $_SESSION['answer']){
      echo "The guess you have enter was too high, please try again!";}
    }else{
      if(!ctype_digit($guess)){
        echo "please do not put dots/letters or symbols in your guess!";
      }
    }else{
      if($_POST['guess'] < $_SESSION['answer']){
        echo "===echo===3==="; // to see if the code is working
        $gz = "The guess you have entered was too high, please try again!";
        echo $gz;
        guessAgainForm();
      }
    }else{
      if($_POST['guess'] == $_SESSION['answer']){
        $gz = "You have guessed the exact number of jelly beans     Congratulations!";
        echo $answer;
        echo $gz;
        playAgainForm();
  }
}}
 ?>




<?
function guessAgainForm(){

?>
<form action="<?=$_SERVER['php_self']?>" method="post">
  <input type="text" name="guess" placeholder="guess!"/>
  <p><button type="submit" name="submit" value="guess"> Guess! </button></p>
</form>
<?
}
?>


<?
function submitAgainForm(){
?>
<form action="<?=$_SERVER['php_self']?>" method="post">
  <input type="text" name="amount" placeholder="Add your jellybeans"/>
  <input type="submit" name="submit" value="add">
</form>
<?
}
?>

<?
function playAgainForm(){
?>
<br>
 <a href="https://
<REDACTED>> Play Again !    </a>

  <?
}
  ?>



</body>
</html>

每个 if-elsif-else 语句只能有一个 else。只需将 else 中有 if 条件的地方更改为 elseifs,就可以了。例如:

} elseif ($_POST['guess'] > $_SESSION['answer']) {
    echo "The guess you have enter was too high, please try again!";
}
// etc...

只需替换以下代码。并且还使用 elseif 条件。你每次都用else.

 if(!$_POST["submit"]){
    SESSION_DESTROY();

到...

  if(!$_POST["submit"]){
    SESSION_DESTROY();
  }