##### Actions

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 over the next few weeks. Thanks for all your past and future contributions.

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

# HTML & PHP - Number guessing game - simple

Article
Created: Maveric (27 Jun 2012)
Last edited: BuildNokia (12 Jun 2014)

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

## Introduction

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.

## Prerequisites

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

## Implementation

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

`<html><head><title>Number guessing game example</title></head><body><?phpif(isset(\$_POST['posted'])){	\$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";							}						else						{							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" /></form></body></html>`

## Summary

You could easily extend this game.