×
Namespaces

Variants
Actions

How to create a simple number guessing game using PHP

From Nokia Developer Wiki
Jump to: navigation, search
Article Metadata
Article
Created: Maveric (12 Mar 2012)
Last edited: hamishwillee (31 Jul 2012)

Introduction

In this article we will implement a simple number guessing game. It goes out with different names, but is basically about higher or lower; the user will be shown a number, selected randomly by the computer. User is to guess if the next number will be less or greater than current one. So, if you are good with intuition, maybe you could even foresee...or just use your instinct...

Prerequisites

-PHP installed on a localhost or remote server. -Code editor of your choise

Example code

For this game, we will be creating three files; index.php, game.php and logic.php. Please rename the files as you wish, combine the code into one file, whatever way you want to modify edit this example code is fullly up to you.

index.php

<?php
$number = rand(1,10);
?>
<html>
<h2>Higher Or Lower Game</h2>
<form action="play.php" method="POST">
<input type="hidden" value="0" name="score">
<input type="hidden" value="<?php echo $number;?>" name="number">
<input type="submit" value="Click to play">
</form>

game.php

<?php
$number = $_POST['number'];
$score =- $_POST['score'];
 
echo "The number is: $number
<p>
<form action='process.php' method='POST'>
<input type='hidden' value='$number' name='number'>
<input type='hidden' value='$score' name='score'>
<input type='submit' value='Higher' name='option'>
</form>
 
<form action='process.php' method='POST'>
<input type='hidden' value='$number' name='number'>
<input type='hidden' value='$score' name='score'>
<input type='submit' value='Lower' name='option'>
</form>
<p>
<Current Score: $score
"
;
 
?>


logic.php

<?php
$option = $_POST['option'];
if($option == "Higher")
{
$score = $_POST['score'];
$number = $_POST['number'];
$new_number = rand(1,10);
 
if($new_number >= $number)
{
$new_score = $score + 1;
echo "The number was higher
<form action='play.php' method='POST'>
<input type="
'hidden' value='$new_number' name='number'>
<input type="'hidden' value='$new_score' name='score'>
<input type="
'submit' value=''Click here to continue'>
</form>";
}
else
{
die("The number was: $number <br>
The new number is: $new_number <p>
Sorry the number was lower<br>
<a href='
index.php'>Click here to play again</a>");
 
 
}
 
}
$option = $_POST['
option'];
 
if($option == "Lower")
{
$score = $_POST['
score'];
$number = $_POST['
number'];
$new_number = rand(1,10);
 
if($new_number <= $number)
{
$new_score = $score + 1;
echo "The number was lower
<form action='
play.php' method='POST'>
<input type="'
hidden' value='$new_number' name='number'>
<input type="'
hidden' value='$new_score' name='score'>
<input type="'
submit' value=''Click here to continue'>
</form>";
}
else
{
die("
The number was: $number <br>
The new number is: $new_number <p>
Sorry the number was higher<br>
<a href='index.php'>Click here to play again</a>");
 
 
}
 
}
?>

Session can be created for the refresh but this is a very simple version.

This page was last modified on 31 July 2012, at 10:31.
171 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×