Page 1 of 1

How to put args in php?

Posted: Tue Nov 07, 2006 10:47 am
by pLachance
Hi,

Im new to php but i know programming like java and c... what i want to know here is how to pass arguments to a php file... like

http://www.myphp.com/index.php arg1 arg2 arg3

Code: Select all

<?php
    // Here i want to access args
    $var1=arg1
    $var2=arg2
    $var3=arg3
?>
i searched but found no topics on this... can someone tell me if there is a way to do that and how to do it.

thanks in advance!

Posted: Tue Nov 07, 2006 11:14 am
by Luke

Posted: Tue Nov 07, 2006 1:10 pm
by pickle
~Ninja's right for web accessible scripts. But If you're working over the command line, & you call the script with flags, you can reference them in $_SERVER['argv']:

Code: Select all

?> ./myPHPscript.php -firstArg -secondArg
then your code can reference $_SERVER['argv'],

$_SERVER['argv'][0] would = myPHPscript.php
$_SERVER['argv'][1] would = -firstArg
$_SERVER['argv'][2] would = -secondArg

Posted: Tue Nov 07, 2006 1:47 pm
by pLachance
thanks a lot :)

Posted: Tue Nov 07, 2006 1:55 pm
by Chris Corbyn
pickle wrote:~Ninja's right for web accessible scripts. But If you're working over the command line, & you call the script with flags, you can reference them in $_SERVER['argv']:

Code: Select all

?> ./myPHPscript.php -firstArg -secondArg
then your code can reference $_SERVER['argv'],

$_SERVER['argv'][0] would = myPHPscript.php
$_SERVER['argv'][1] would = -firstArg
$_SERVER['argv'][2] would = -secondArg
$_SERVER ? I thought it was just $argv. I hope I haven't been working with register_globals for CLI :(

Posted: Tue Nov 07, 2006 1:59 pm
by volka
d11wtq wrote:$_SERVER ? I thought it was just $argv. I hope I haven't been working with register_globals for CLI :(
see http://de2.php.net/manual/en/features.c ... hp#AEN7923

Posted: Tue Nov 07, 2006 2:05 pm
by pickle
I honestly didn't know $argv was defined, I just checked $_SERVER & the flags were passed, so I've been using that.

Thanks for the heads up though.

Posted: Tue Nov 07, 2006 6:57 pm
by timvw
Personally i find getopt an easier way to get options from the command line...