Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries. Thanks for all your past and future contributions.

Revision as of 07:25, 31 July 2012 by hamishwillee (Talk | contribs)

HTML & PHP - Number guessing game - simple

From Wiki
Jump to: navigation, search
Article Metadata
Created: Maveric (27 Jun 2012)
Last edited: hamishwillee (31 Jul 2012)

This article explains how to create a simple number guessing game in HTML and PHP.



This article shows how to create a number guessing game with HTML and embedded PHP. The game concept is simple; the user is to guess a computer generated "secret" number between 1 and 10. User will be told if the guess was lower or higher than the "secret" number.


  • Code editor of your choise
  • PHP installed
  • Web browser


We will only need to create one file for this example, and we are saving it with the name "game.php". You can choose any name you want ofcourse.

Example code

<title>Number guessing game example</title>
$user_guess = $_POST['guess'];
$number = rand(1,10);
if($user_guess > $number)
echo "<h2>The number you guessed is higher than the secret: $number</h1><br/>";
echo "Try again";
else if($user_guess < $number)
echo "<h1>The number you guessed is lower than the secret: $number</h1>";
echo "Try again";
echo "<h1>You had guessed right!"</h1>
<form action="game.php" method="post">
Please try to guess what is the secret number, between 1 and 10:
<input type="text" name="guess" /><br />
<input type="hidden" name="true" name="posted" />
<input type="submit" value="submit" />

Tested on

Google Chrome 19.0.1084.52 m


You could easily extend this game.

311 page views in the last 30 days.