dtyson2000 Posted April 26, 2009 Share Posted April 26, 2009 I've been going crazy over this for a week. I have a page with two functions on it. One sets a random color value and asks for the user to input the value given. The second function is supposed to check the value input by the user and compare it to the randomly generated color value. I have read about variable scope and the variables appear to be in the right place. The problem is that in declaring a global variable outside of the functions, when the check happens, the page reloads and the random color value changes, therefore one always gets an incorrect match. I thought I had this and understood but I don't. Can anybody offer a pointer? Thanks for your time, in advance! // color bank array $colorbank = array("red", "blue", "orange", "green", "purple"); // grab random value from color bank $randcolor = array_rand($colorbank, 1); // check for user submission on page load if (isset($_POST['check'])){ check(); } else { getcolor(); } function getcolor(){ global $colorbank, $randcolor; echo " <form action='' method='POST'> <p>Type the following color in the box: $randcolor <table> <tr> <td>Number</td> <td><input type='textbox' name='color_user'></td> </tr> </table> <p><input type='submit' name='check' value= 'check'> <input type='reset' value='reset'> </form> </div>"; } function check() { global $colorbank, $randcolor; if ($color_user == "$randcolor"){ echo "Correct!<br />"; } else { echo "Incorrect!<br />"; } } Link to comment https://forums.phpfreaks.com/topic/155676-i-give-up-variable-scope/ Share on other sites More sharing options...
aeonsky Posted April 26, 2009 Share Posted April 26, 2009 Where do you get $color_user in check(){} ? Link to comment https://forums.phpfreaks.com/topic/155676-i-give-up-variable-scope/#findComment-819372 Share on other sites More sharing options...
dtyson2000 Posted April 26, 2009 Author Share Posted April 26, 2009 It's coming from the user input in the function getcolor{}. Link to comment https://forums.phpfreaks.com/topic/155676-i-give-up-variable-scope/#findComment-819377 Share on other sites More sharing options...
aeonsky Posted April 26, 2009 Share Posted April 26, 2009 Ok then, but its you didn't set it to anything... How about... $color_user = $_POST['color_user']; I'd also would like to discourage you from using globals. In higher level languages they are forbidden (from my experience anyways). Link to comment https://forums.phpfreaks.com/topic/155676-i-give-up-variable-scope/#findComment-819378 Share on other sites More sharing options...
PFMaBiSmAd Posted April 26, 2009 Share Posted April 26, 2009 Web servers are stateless. They don't know or care what happened on any page request before or after the current one. To cause a value generated on one page request to exist on another page request you need pass that value to the new page. $randcolor will be generated on each page request. If you want the one that was generated when the form was requested to exist when the form is processed you need to pass it in a session variable. Also, don't use the global keyword. If you in fact have functions and variables that are so closely related that the are always used together, you should be using a class. Link to comment https://forums.phpfreaks.com/topic/155676-i-give-up-variable-scope/#findComment-819381 Share on other sites More sharing options...
PFMaBiSmAd Posted April 26, 2009 Share Posted April 26, 2009 You are also going to have a problem in accessing the color_user input from the form. You need to use $_POST['color_user'] as $color_user won't be set since register_globals were turned off by default 7 years ago in php4.2. Link to comment https://forums.phpfreaks.com/topic/155676-i-give-up-variable-scope/#findComment-819439 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.