1. Technology
You can opt-out at any time. Please refer to our privacy policy for contact information.

Guess The Number Tutorial


2 of 3

Possible Outcomes
 else {
 //this runs if the game is already in progress

 if ($Num >$x)
 {Echo "Your number, $Num, is too high. Please try again<p>";}
 //if the number they guessed is bigger than x, it lets them know their guess was too high

 elseif ($Num == $x)
 {Echo "Congratulations you have won!<p>";
 //if the number they guessed was correct it lets them know they won
 Echo "To play again, please Choose a Number 1-100 <p>";
 $x = rand (1,100) ;
 //it then starts the game again by choosing a new value for $x that they can guess

 {Echo "Your number, $Num, is too low. Please try again<p>";}
 //if the answer is neither correct or to high, it tells them it is too low
This section of code compares the winning number $x, to the number the user chose. It then either tells the user their guess was too high, too low, or they have won. If they win, it chooses another $x so they can play again.
Related Video
Basic PHP Syntax
Using PHP With HTML
  1. About.com
  2. Technology
  3. PHP / MySQL
  4. Step By Steps
  5. Guess Random Number - PHP Guess Rand - Guess Number PHP

©2014 About.com. All rights reserved.