这是我目前的代码:
时不是自我毁灭
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://s215028.brambling.cdu.edu.au/jbeantest.php"> Play Again ! </a>
<?
}
?>
</body>
</html>
答案 0 :(得分:1)
每个if-elsif-else语句只能有一个else
。只需更改if
条件中else
到elseif
的位置,您就可以了。 E.g:
} elseif ($_POST['guess'] > $_SESSION['answer']) {
echo "The guess you have enter was too high, please try again!";
}
// etc...
答案 1 :(得分:0)
只需更换以下代码即可。并且还使用elseif
条件。您每次都使用else
。
if(!$_POST["submit"]){
SESSION_DESTROY();
要...
if(!$_POST["submit"]){
SESSION_DESTROY();
}