Jump to content

[SOLVED] random number - using variable as max value


Recommended Posts

<?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.

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

 

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.

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

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

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?

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!

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.