Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I know that you've already accepted an answer, but I went ahead and coded you an example based off of your script. I figure if anything, it will help you in the future. There were many syntactical mistakes throughout your script. If you coded verbatim from your book, I would get rid of the book and look for better sources.</p> <p>I took the liberty to modify a few things. If you have any questions, feel free to contact me. </p> <p>This is a working copy of your game with a lot of changes throughout the script. </p> <p>It works. Just save the code as guessing.php and run it in your browser.</p> <pre><code>&lt;?php // Set inital variables // // Guesses for form // $guessLeft = 5; // Output message // $message = ''; // Whether to show the form or not // $showForm = true; // Help message to help user decide where to go, // Up or down, and how many guesses // $helpMessage = ''; // Displays a fail message // function displayFail($randNum) { $message = "You ran out of Guesses! My number was {$randNum}."; return $message; } // Displays a success message // function displaySuccess($randNum) { $message = "You guessed it! {$randNum} is correct!"; return $message; } // Displays a help message // function helpMessage($help, $guessLeft) { // Check plural // $plu = ($guessLeft &gt; 1) ? "tries" : "try"; // Format Output message // $message = '&lt;p&gt;'; $message .= $help; $message .= " You have {$guessLeft} {$plu} left to guess correctly"; $message .= '&lt;/p&gt;'; return $message; } // Check that submit button has been set // if (isset($_POST["submitButton"]) and isset($_POST["guess"])) { // Collect $_POST information // $randNum = (int) $_POST["randNum"]; $guessLeft = (int) $_POST["guessLeft"]; $guess = (int) $_POST["guess"]; // Check user guess // if ($guess &lt; $randNum) { // Number too low // // Decrease guesses left // $guessLeft -=1; $helpMessage = helpMessage('Too low, try again.', $guessLeft); } elseif ($guess &gt; $randNum) { // Number too high // // Decrease guesses left // $guessLeft -=1; $helpMessage = helpMessage('Too high, try again.', $guessLeft); } else { // Number correct, hide form show message // $showForm = false; $message = displaySuccess($randNum); } // If there are no more guesses left // // Hide page and display fail message // if ($guessLeft == 0) { $showForm = false; $helpMessage = ''; $message = displayFail($randNum); } } ?&gt; &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt; &lt;html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"&gt; &lt;head&gt; &lt;title&gt;Guessing Game&lt;/title&gt; &lt;link rel="stylesheet" type="text/css" href="common.css" /&gt; &lt;/head&gt; &lt;body&gt; &lt;?php // Show help message if it is set // // Else display nothing // if (strlen($helpMessage)) { echo $helpMessage; } ?&gt; &lt;?php if ($showForm) { ?&gt; &lt;form action="guessing.php" method="post"&gt; &lt;input type="hidden" name="randNum" value="&lt;?php echo (isset($_POST['randNum'])) ? $_POST['randNum'] : rand(1, 100); ?&gt;" /&gt; &lt;input type="hidden" name="guessLeft" value="&lt;?php echo $guessLeft; ?&gt;" /&gt; &lt;label for="guess"&gt;What is your guess?&lt;/label&gt; &lt;input type="text" name="guess" id="guess" value="" /&gt;&lt;br /&gt; &lt;input type="submit" name="submitButton" value="Guess" /&gt; &lt;/form&gt; &lt;?php } else { // Output Message // echo $message; } ?&gt; &lt;/body&gt; &lt;/html&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload