1. Home
  2. Computing & Technology
  3. PHP / MySQL

Guess The Number Tutorial

By Angela Bradley, About.com

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
}

else
{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.
Explore PHP / MySQL
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. PHP / MySQL
  4. Step By Steps
  5. Guess Random Number - PHP Guess Rand - Guess Number PHP

©2009 About.com, a part of The New York Times Company.

All rights reserved.