|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Hi all,
When I first load the file, an error msg of undefined index: guess pop up. After the first guess, the error msg is gone. Another problem, the num_tries won't increment. Can anybody help me to solve these problem? <?php $num_to_guess = 42; Static $num_tries = 0; $num_tries = (isset($_POST["num_tries"])) ? $num_tries+=1 : 0; $message = ""; if (!isset($_POST["guess"])) { $message = "Welcome to the guessing machine!"; } elseif ($_POST["guess"] > $num_to_guess ) { $message = "$_POST[guess] is too big! Try a smaller number"; } elseif ($_POST["guess"] < $num_to_guess ) { $message = "$_POST[guess] is too small! Try a larger number"; } else { // must be equivalent $message = "Well done!"; } $guess = (int) $_POST["guess"]; ?> <html> <body> <h1> <?php print $message ?> </h1> Guess number: <?php print $num_tries?> <form action="<?php print $_SERVER["PHP_SELF"] ?>" method="POST"> Type your guess here: <input type="text" name="guess" value="<?php print $guess?>"> <input type="hidden" name="num_tries" value="<?php print $num_tries?>"> </form> </body> Thx a lot! aqua ![]() |
|
#2
|
|||
|
|||
|
i would do it in a loop much more efficent
|
|
#3
|
||||
|
||||
|
The message you're getting is only a warning. Most production servers have these types of warnings turned off...
However, an easy way to fix this would be to put the entire bulk of the program into an if statement... I would re-write your code like this: PHP Code:
|
|
#4
|
|||
|
|||
|
Increment problem!
The problem on incrementing num_tries. Anybody knows how to solve it?
|
![]() |
| Viewing: Dev Articles Community Forums > Programming > General Programming Help > Undefined index and variable |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|