Believe it or not, but the last 13 or so posts have all been leading up to this one. Generating random numbers, capturing user input, using colors on the command-line, pluralizing words, and then some are all being utilized in this simple number guessing game.
The script itself generates a random number between 1 and 100, asks the user to guess, advises them if they are too high or too low then tells them how many guesses it took them to guess the number:
<?php
$number = rand(1, 100
$guesses = 0
$won = false
$handle = fopen('php://stdin', 'r'
echo "nI'm thinking of a number between 1 and 100...nCan you guess the number?nn"
while (!$won)
{
$guesses++
echo 'What is your guess? '
$guess = trim(fgets($handle
if (preg_match('/d/', $guess))
{
if ($guess > $number)
{
echo "