I'm totaly new to PHP and I'm trying to create this simple game as an exercise for Loops and combining PHP with HTML.
The current questions that I have:
How do I stop the loop executing before the answer is set. Also why does it still echos the "Please enter your answer" even though the "IF" statement checks for if the Answer is set or not. And lastly how can I see which number has the "RAND" function chosen? If I simply add "ECHO $direction it will show me the previous value."
Thank you.
<?php
//I will be adding a condition where the user will be able to stop the game
while (some statement)
{
?>
//It shows the form to the user
<form method="POST">
<p> Answer with 0 or 1</p>
<input type="text" name="answer" />
<input type="submit" value="Submit"/>
</form>
//The answer is set
<?php
$direction = rand(0,1);
//Checks if the answer is set and if the text area isn't empty
if (isset($_POST['answer']))
{
if (!empty($_POST['answer']))
{
if ($_POST['answer'] == $direction)
{
echo 'You guessed right!';
}
else
{
echo 'Your guess is incorrect!';
}
}
else
{
echo 'Please enter your answer';
}
}
}
?>
Aucun commentaire:
Enregistrer un commentaire