Click here to Skip to main content
15,920,217 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i want to echo "higher" when guess is higher than computer generated number and echo "lower" when guess lower than computer generated number. echo "congratulations" when guess equals computer generated number. users has six attempts. Game ends.

can't figure out the challenge from my code.

thanks in advance..

What I have tried:

PHP
DOCTYPE html>


<title>php file 




welcome <?php echo $_post["firstname"]'.'["lastname"] ?> !!,  </br >
<p>Glade you are here, let play this guessing game, the computer has generated random numbers</p>
<p> between 0 - 20, guess the computer generated number and see if your guess is right!</p>
  
  <br>
  <br>
  


<h3>Guess A Number Between 0 and 20:</h3>


<p>Outcome:</p>
	


<?php

$rand = mt_rand(0,20);
if (isset($_post["number_accepted"])) 
{
$submit = $_post["number_accepted"];

if ($submit > 20 || $number_accepted < 0) {
echo "guess should be between 0 and 20";
}

elseif ($number_accepted > $rand) 
{
echo "guess to high";
}
elseif ($number_accepted < $rand)
{
echo "guess too low";
}
else
 {
echo "congratulations <?php echo $_post["firstname"]'.'["lastname"] ?>"
}
Posted
Updated 9-Dec-18 11:07am
v2

1 solution

Not clear what not working. Couple of suggestion

1. I think the _POST keyword is case sensitive, replace _post with _POST
2. $number_accepted, not clear where it being declared, maybe $submit should be $number_accepted?
3. look like there some syntax error at the else statement.

PHP
<?php

$rand = mt_rand(0,20);
if (isset($_POST["number_accepted"])) 
{
	$number_accepted = $_POST["number_accepted"];

	if ($number_accepted > 20 || $number_accepted < 0) {
		echo "guess should be between 0 and 20 <br/>";
	}

	elseif ($number_accepted > $rand) 
	{
		echo "guess to high <br/>";
	}
		elseif ($number_accepted < $rand)
	{
		echo "guess too low <br/>";
	}
	 else
	 {
		echo "congratulations " .  $_POST["firstname"] . ' ' . $_POST["lastname"];
	 }
}

?>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900