Note that there are some explanatory texts on larger screens.

plurals
  1. POPart of my script keeps appearing in the browser and it doesn't work properly, what am I doing wrong?
    text
    copied!<p>I'm currently working my way through a beginners php book and am trying to do an exercise that wants me to make a number guessing game. After failed attempts on my own I looked to the appendix for the answer. I've typed this code out multiple times and I believe is character for character what the answer key has and yet part of the script keeps appearing in the browser when I run the script. What am I missing? Thanks!</p> <pre><code>&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;&lt;/title&gt; &lt;link rel="stylesheet" type="text/css" href="common.css" /&gt; &lt;/head&gt; &lt;body&gt; &lt;?php if ( isset($_POST["submitButton"] ) and isset( $_POST["guess"] ) ) { processForm(); } else { displayForm( rand( 1, 100 ) ); } function processForm() { $randNum = (int)$_POST["randNum"]; $guessLeft = (int)$_POST["guessLeft"]; $guess = (int)$_POST["guess"]; if ( $guess == $randNum ) { displaySuccess( $randNum ); } elseif ( $guess == 0 ) { displayFail( $randNum ); } elseif ( $guess &lt; $randNum ) { displayForm( $randNum, $guessLeft, "Too low, try again."); } else ( $guess &gt; $randNum ) { displayForm( $randNum, $guessLeft, "Too high, try again."); } } function displayForm( $randNum, $guessLeft=5, $message="") { ?&gt; &lt;form action="" method="post"&gt; &lt;div&gt; &lt;input type="hidden" name="randNum" value="&lt;?php echo $randNum?&gt;" /&gt; &lt;input type="hidden" name="guessLeft" value="&lt;?php echo $guessLeft?&gt;" /&gt; &lt;?php if ( $message ) echo "&lt;p&gt;$message&lt;/p&gt;" ?&gt; &lt;p&gt;Guess my number. You have &lt;?php echo $guessLeft?&gt; &lt;?php echo ( $guessLeft == 1 ) ? "try" : "tries"?&gt; left to guess correctly.&lt;/p&gt; &lt;p&gt;What's your guess? &lt;input type="text" name="guess" value="" style="float: none; width: 3em;" /&gt; &lt;input type="submit" name="submitButton" value="Guess" style="float: none;" /&gt;&lt;/p&gt; &lt;/div&gt; &lt;/form&gt; &lt;?php } function displaySuccess( $randNum ) { ?&gt; &lt;p&gt;You guess it! &lt;?php echo $randNum?&gt; is correct!&lt;/p&gt; &lt;form action="" method="post"&gt; &lt;p&gt;&lt;input type="submit" name="tryAgain" value="Try Again" style="float: none;" /&gt;&lt;/p&gt; &lt;/form&gt; &lt;?php } function displayFail( $randNum ) { ?&gt; &lt;p&gt;You ran out of guesses! My number was &lt;?php echo $randNum?&gt;&lt;/P&gt; &lt;form action="" method="post"&gt; &lt;p&gt;&lt;input type="submit" name="tryAgain" value="Try Again" style="float: none;" /&gt;&lt;/p&gt; &lt;/form&gt; &lt;?php } ?&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