【问题标题】:unexpected else (T_ELSE)其他意外(T_ELSE)
【发布时间】:2016-12-03 09:27:00
【问题描述】:

这是我当前的代码:

它不会自毁

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>

【问题讨论】:

标签: php html forms syntax-error


【解决方案1】:

每个 if-elsif-else 语句只能有一个 else。只需将具有if 条件的位置更改为elseelseifs,就可以了。例如:

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

【讨论】:

    【解决方案2】:

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

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

    到...

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-10-03
      • 2012-01-06
      • 2020-12-04
      • 1970-01-01
      • 2010-12-10
      • 1970-01-01
      • 2017-05-01
      • 1970-01-01
      相关资源
      最近更新 更多