Alanistic Posted September 7, 2008 Share Posted September 7, 2008 Hi All, I've just started teaching myself PHP using "PHP 5 In Easy Steps" by Mike McGrath, and one of the php scripts isn't running as expected. I've gone through the code several times and as far as I can see everything is correct. The code prompts for a number between 1 and 20, and should either display too high, too low, or invalid. Unfortunately, it seems to be getting stuck at the invalid bit and no matter what number I specify it always says invalid entry. Is there anything obvious that I've done wrong? <?php header ( "Cache-Control:no-cache" ); /* Use cache control to ensure that the page is reloaded with each visit and is not re-loaded from the browsers cache. */ /* setnum function to randomly generate a random number between 1 and 20 and set this to the $num variable. */ function setnum() { global $num; srand( (double)microtime() * 100000 ); $num = rand( 1, 20 ); } ?> <html> <head> <title> Number Guess </title> </head> <body> <?php # Assigns values from form. $num = $_POST['num']; $self = $_SERVER['PHP_SELF']; # Displays starting instructions. if( $num == null) { $msg = "I have thought of a number between 1 and 20"; $msg .= " <h3>guess what it is ...</h3>"; } # Error message for non numeric guesses if( $num != null and !is_numeric($guess) ) { $msg = "Your guess was invalid<h3>Try again!</h3>"; } # Check to see if the guess is correct. else if ( $guess == $num ) { if ( $num != null) { $msg = "CORRECT! The number was $num"; $msg .= "<h3><a href = \"$self\" >"; $msg .= "CLICK HERE TO TRY AGAIN???</a></h3>"; } setnum(); } # Check to see if guess is higher than the number. else if ( $guess > $num ) { $msg = "You guessed $guess<h3>My number s lower!</h3>"; } # Check to see if the guess is lower than the number. else if ( $guess < $num ) { $msg = "You guessed $guess<h3>My number is higher!</h3>"; } # Display message. echo ( $msg ); ?> <!-- Uses HTML code to call $self variable --> <form action = "<?php $self ?>" method = "post"> <input type = "hidden" name = "num" value = "<?php echo( $num ); ?>" > Guess:<input type = "text" name = "guess"> <input type = "submit" value = "Submit"> </form> </body> </html> Link to comment https://forums.phpfreaks.com/topic/123124-simple-help-question/ Share on other sites More sharing options...
GingerRobot Posted September 7, 2008 Share Posted September 7, 2008 If that's the code from the book, i suggest you buy a different one. It's terrible. As for the problem, $guess is undefined. You need to take it out of the $_POST superglobal first: $guess = $_POST['guess']; Link to comment https://forums.phpfreaks.com/topic/123124-simple-help-question/#findComment-635925 Share on other sites More sharing options...
Alanistic Posted September 7, 2008 Author Share Posted September 7, 2008 If that's the code from the book, i suggest you buy a different one. It's terrible. As for the problem, $guess is undefined. You need to take it out of the $_POST superglobal first: $guess = $_POST['guess']; Thanks for the feedback. That solved my problem. 1 tiny mistake. You mentioned the code is terrible, what makes it bad code? Link to comment https://forums.phpfreaks.com/topic/123124-simple-help-question/#findComment-635931 Share on other sites More sharing options...
GingerRobot Posted September 7, 2008 Share Posted September 7, 2008 You mentioned the code is terrible, what makes it bad code? Global variables, odd logic, weird HTML comments etc. Dont get me wrong, if this is something you've written yourself as you're learning, then don't worry about that. My point was that it would not be good code to be learning from. Link to comment https://forums.phpfreaks.com/topic/123124-simple-help-question/#findComment-635944 Share on other sites More sharing options...
Alanistic Posted September 7, 2008 Author Share Posted September 7, 2008 All the comments were added by myself to follow the program. I'm normally a java programmer so all the concepts of php I can handle, its just trying to get to grips with most of the syntax. How would you code that if you were tackling it? What would you change? Link to comment https://forums.phpfreaks.com/topic/123124-simple-help-question/#findComment-635947 Share on other sites More sharing options...
PFMaBiSmAd Posted September 7, 2008 Share Posted September 7, 2008 I'm with GingerRobot on that code being a bad example to be learning from. The setnum() function is using the global keyword to access $num instead of properly being it into the function as a parameter and return it as a result. The php code in the <form action="...." parameter is not actually echoing the variable $self. The form only works because an empty "" action value causes the form to submit to the same page. Link to comment https://forums.phpfreaks.com/topic/123124-simple-help-question/#findComment-635953 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.