Home > PHP > Examples > Guess my number

Guess my number

Submit a blank guess to start again.

I am thinking of a number between 1 and 1000

Enter your guess:

you did not make a guess I will pick a new number.

 

// 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. (2016, July 04, 04:31 pm). Guess my number.
     Retrieved November 20, 2017, from
     http://www.rupert.id.au/php/examples/guess/index.php

1067 Visits since October 23, 2004