Page 1 of 1

newbie question accessing argv

Posted: Mon May 26, 2008 12:40 pm
by millerthegorilla
Hi there - is there any way to access the a variable name passed to a php script as follows

url/test.php?gorilla

What I want to do is display the word that is passed to the script(in the case above it would be 'gorilla'). I don't believe I can use $_REQUEST for this as it is an associative array. I've tried $_SERVER['argv'] with an index of 0 but with no result.

Thanks in advance.

Re: newbie question accessing argv

Posted: Mon May 26, 2008 1:24 pm
by impulse()
Try putting

Code: Select all

print_r($_GET)
into your code.

Hope that helps,

Re: newbie question accessing argv

Posted: Mon May 26, 2008 1:25 pm
by impulse()
Also, just to add. The argv variable is for CLI-PHP so it can pick up on parameters passed in from the shell.

EG:

php example.php stephen richards

stephen would then become $_argv[1] and richards would become $_argv[2] in the script. $_argv[0] is reserved for the script name.

Re: newbie question accessing argv

Posted: Mon May 26, 2008 2:42 pm
by millerthegorilla
Thanks for your response but using $_GET won't work any more than $_REQUEST. They are both associative arrays so I can't use an index to access the first entry. It has to be accessed using the name of a name/value pair ie $_GET['name'] will return the value.
What I want to do is access the first element of the array with an index. I am not using the command line, I am using a server side script but I thought you could access the argv variable of $_SERVER as follows: $_SERVER['argv'] which I thought in this case would return 'name'. But it doesn't. I'm not sure if there's a way to do this as I think php is designed to be passed name/value pairs from an http submit.

Re: newbie question accessing argv

Posted: Mon May 26, 2008 2:46 pm
by millerthegorilla
I've just sussed it out. You need to use $_SERVER[QUERY_STRING].