blue-genie Posted September 14, 2009 Share Posted September 14, 2009 <?php session_start(); function getRandom() { $maxVal= $_REQUEST['maxValue']; $random_number = rand(0, $maxVal); echo "$random_number"; } getRandom(); ?> if i test this with testRandom.php?maxVal=100 for example i keep getting 0 i'm not getting any syntax errors but if i hardcode the max value it works as expected. What am i doing wrong? blu. Quote Link to comment https://forums.phpfreaks.com/topic/174172-solved-random-number-using-variable-as-max-value/ Share on other sites More sharing options...
Zane Posted September 14, 2009 Share Posted September 14, 2009 well for one... you are asking for $maxVal to equal $_REQUEST['maxValue'] better yet...you are asking for it to equal $_GET['maxValue'] This index doesn't exist..from what you've shown.. you should be getting an undefined index error. you need it to be $_REQUEST['maxVal'] That's the first catch. Secondly.. you should NEVER use your superglobals in a function anyway..by superglobals I mean things like $_REQUEST $_GET $_POST $_COOKIE $_SESSION $_FILES Quote Link to comment https://forums.phpfreaks.com/topic/174172-solved-random-number-using-variable-as-max-value/#findComment-918152 Share on other sites More sharing options...
blue-genie Posted September 14, 2009 Author Share Posted September 14, 2009 thanks for the response zanus. don't know if this is pertinent but I'm parsing the value through to PHP from flash, hence the _REQUEST but to test my php i use the query ?maxVal in the browser to check that my PHP works first. and in this case it's not working. Quote Link to comment https://forums.phpfreaks.com/topic/174172-solved-random-number-using-variable-as-max-value/#findComment-918159 Share on other sites More sharing options...
Zane Posted September 14, 2009 Share Posted September 14, 2009 but to test my php i use the query ?maxVal in the browser to check that my PHP works first. yeah and in your script you are calling this $maxVal= $_REQUEST['maxValue']; notice how you are calling $_REQUEST['maxValue'] instead of $_REQUEST['maxVal'] don't know if this is pertinent but I'm parsing the value through to PHP from flash, hence the _REQUEST not to pertinent, but nevertheless...the variable is in the URL and variables in a url are part of the GET variable scope..so....you should use the GET variable scope. There is also the POST variable scope but instead you are using the REQUEST variable scope...which isn't a bad thing...just bad practice..and bad learning...because REQUEST contains...both of them....and cookie Description of $_REQUEST An associative array that by default contains the contents of $_GET, $_POST and $_COOKIE. short answer change $_REQUEST['maxValue'] to $_GET['maxVal'] or $_RESUEST['maxVal'] if you choose Quote Link to comment https://forums.phpfreaks.com/topic/174172-solved-random-number-using-variable-as-max-value/#findComment-918161 Share on other sites More sharing options...
blue-genie Posted September 14, 2009 Author Share Posted September 14, 2009 ok so in theory if I do this <?php session_start(); $maxVal= $_GET['maxValue']; function getRandom() { $random_number = rand(0, $maxVal); echo "$random_number"; } getRandom(); ?> i should then be able to test it in the brower like this http://host/testRandom.php?maxValue=200 right? I still get 0 Quote Link to comment https://forums.phpfreaks.com/topic/174172-solved-random-number-using-variable-as-max-value/#findComment-918163 Share on other sites More sharing options...
blue-genie Posted September 14, 2009 Author Share Posted September 14, 2009 ok wait I'm confused, you said i musn't use super globals in my function so i take $maxVal=$_GET['maxValue']; out of the function - doesn't work if i put it in the function it works. so what's the work around? Quote Link to comment https://forums.phpfreaks.com/topic/174172-solved-random-number-using-variable-as-max-value/#findComment-918172 Share on other sites More sharing options...
Zane Posted September 14, 2009 Share Posted September 14, 2009 in theory..no postulate..no. just....no This function $myVariable = $_GET['myVal']; //The line above and the lines below are completely unconnected... //Now later on...you may say to yourself...."Hey $myVariable would fit very nicely where $theArgument is!" function getRandom($theArgument) { $random_number = rand(0, $theArgument); echo "$random_number"; // The quotes you have here are useless } like ANY function ... can take an argument. it can take an unlimited number of them. I doubt you're going to try to prove me wrong or find the documentation on the unlimited part of that statement because I'm not even sure...but I'm sure it's quite a bit...depending on the language Now...since we've given this function an argument at it's.....composition stage I'll call it...we have to CALL the function with the same number of arguments. so... since $myVariable has the stuff we need in it...we'll pass it to the function. getRandom($myVariable); poof! Quote Link to comment https://forums.phpfreaks.com/topic/174172-solved-random-number-using-variable-as-max-value/#findComment-918175 Share on other sites More sharing options...
blue-genie Posted September 14, 2009 Author Share Posted September 14, 2009 ah ok i think she's got it. thanks :-) Quote Link to comment https://forums.phpfreaks.com/topic/174172-solved-random-number-using-variable-as-max-value/#findComment-918180 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.