Home > PHP > Examples > Guess my number
Submit a blank guess to start again.
// Based on code from John de Figueiredo
<?php
$guess = $_POST [guess];
$lowerlimit = $_POST[lowerlimit];
$upperlimit = $_POST[upperlimit];
$hiddennumber = $_POST[hiddennumber];
if (empty($guess)) {
$lowerlimit = 1;
$upperlimit = 1000;
$hiddennumber = rand(1,1000);
} // end if
$output = "<H2>I am thinking
of a number between ";
$output .= "$lowerlimit and
$upperlimit</H2>";
echo $output;
echo "the hidden number is
". $hiddennumber;
echo <<<HERE
<FORM METHOD="POST"
ACTION="index.php">
<INPUT TYPE="hidden"
NAME="lowerlimit"
VALUE="$lowerlimit">
<INPUT TYPE="hidden"
NAME="upperlimit"
VALUE="$upperlimit">
<INPUT TYPE="hidden"
NAME="hiddennumber"
VALUE="$hiddennumber">
<P>Enter your guess:
<INPUT TYPE="text"
NAME="guess"></P>
HERE;
if (empty($guess)){
echo "you
did not make a guess ";
echo "I
will pick a new number.<br>";
} // end if
else {
echo "your guess is: ".
$guess;
echo "<br>";
if ($guess >
$hiddennumber) {
echo
"your guess is too high <br>";
} // end if
if ($guess <
$hiddennumber) {
echo
"your guess is too low. <br>";
} // end if
if ($guess ==
$hiddennumber) {
echo
"<h1>you win. </h1>";
} // end if
} // end else
echo <<<HERE
<P><INPUT
TYPE="submit"
VALUE="Submit Guess"></P>
</FORM>
HERE;
?>
APA citation:
Russell, R. (2020, December 05, 04:33 am). Guess my number.
Retrieved November 23, 2024, from
http://www.rupert.id.au/php/examples/guess/index.php