How to put args in php?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
pLachance
Forum Newbie
Posts: 4
Joined: Tue Nov 07, 2006 10:28 am

How to put args in php?

Post 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!
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post 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
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
pLachance
Forum Newbie
Posts: 4
Joined: Tue Nov 07, 2006 10:28 am

Post by pLachance »

thanks a lot :)
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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 :(
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

Personally i find getopt an easier way to get options from the command line...
Post Reply