# Novice - Counting Script

Go to solution Solved by Strider64,

## Recommended Posts

My prof hasn't gotten back to me and he doesn't seem like even he knows how to do it so I'm posting here for some help. We've barely done any script writing yet and I can't seem to figure out how to go about doing it. He wants us to modify the existing script so that it generates a random number from 1 - 1000 and then counts the number of guesses and outputs how many guesses at the end. It won't open in the browser so I'm not sure what's wrong. Any help would be much appreciated!

This is what I have so far:

<?php

if (!isset(\$_POST["guess"])) {
\$message = "Welcome to the guessing machine!";
\$_POST["numtobeguessed"] = rand(1,1000);
\$_POST["counter"] = 0;
} else if (\$_POST["guess"] > \$_POST["numtobeguessed"]) {
\$message = \$_POST["guess"]." is too big! Try a smaller number.";
\$_POST["counter"]++;
} else if (\$_POST["guess"] < \$_POST["numtobeguessed"]) {
\$message = \$_POST["guess"]." is too small! Try a larger number.";
\$_POST["counter"]++;
} else { // must be equivalent
\$message = "Well done! It took you '\$_POST["counter"]' tries!";
}
?>
<html>
<title>A PHP number guessing script</title>
<body>
<h1><?php echo \$message; ?></h1>
<form action="" method="POST">
<input type="text" name="guess"></p>
<input type="hidden" name="numtobeguessed"
value="<?php echo \$_POST["numtobeguessed"]; ?>" ></p>

<input type="hidden" name="counter"
value="<?php echo \$_POST["counter"]; ?>"

</form>
</body>
</html>

##### Share on other sites

• Solution

You were close. btw I don't know what you mean it won't open up in the browser for it should had even with an error(s).

```/* You had this */
\$message = "Well done! It took you '\$_POST["counter"]' tries!";
/* It should had been something like this */
\$message = 'Well done! It took you ' . \$_POST["counter"] . ' tries!';```
Edited by Strider64
##### Share on other sites

So this should be opening in browser and appearing correctly?? I suppose that's why the prof was confused as am I.. We've been having issues with the permissions on the server changing. I'll try taking another look at those and try a different browser to open..

Thank you! For anyone else please feel free to point out any other mistakes or syntax errors you find!

##### Share on other sites

Add the lines from my signature to the beginning of your php code and see what errors you are getting.

##### Share on other sites

I got it working!! Thanks so much!! I was worried since I had to submit it by tonight! I will definitely be back here for any more questions in the future!!

##### Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

## Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

×   Pasted as rich text.   Restore formatting

Only 75 emoji are allowed.