Phil Ewington - 43 Plc wrote on donderdag 10 juni 2004 15:11:

>> Phil Ewington - 43 Plc wrote on donderdag 10 juni 2004 14:27:
>> 
>>>> Phil Ewington - 43 Plc wrote on donderdag 10 juni 2004 12:58:
>>>> 
>>>>> Hi All,
>>>>> 
>>>>> Can anyone tell me how to pass arguments to a PHP script from the
>>>>> command line? I know I need to use the -args option but how
>>>>> exactly, I have tried the following...
>>>>> 
>>>>> /usr/bin/php myscript.php -args key=value
>>>> 
>>>> You don't have to call the -args argument. just run the script
>>>> from command line like this: 
>>>> 
>>>> php myscript.php value
>>>> 
>>>> Where value is the argument you want to use in the script.
>>>> These arguments are stored in an arrray ($argv).
>>>> 
>>>> Do a print_r($argv) and you can see the content.
>>> 
>>> OK, but if no param name is passed in, I assume that the order they
>>> are passed in as must be correct or the script will not know what
>>> values belong to which parameters? This seems a little odd, I assume
>>> that $argv only exists when accessed from the command line? My
>>> script needs to be excuted from both command line and from web
>>> browsers. 
>>> 
>> 
>> How do you mean the order must be correct. Use some code like this
>> to give it an index value. for ($counter=0; $counter < $argc;
>>    $counter++) { list($key, $value) = each($argv);
>>    echo ("key ". $key . "has value : " . $value . "\n");
>> }
> 
> Your example was...
> 
> php myscript.php value
> 
> That is just a value, no parameter name as with a query string, take
> this example...
> 
> php myscript.php a b
> 
> Let's say that this was requested from a browser like
> myscript.php?1=a&2=b 
> 
> PHP uses 1 & 2 to refer to the values, but the values would get mixed
> up if I was to use...
> 
> $argv[0] & $argv[1]
> 
> and the order of a and b were different when the script was called.
> Does that make sense? I find it odd that I need to write more code to
> 1) check $_GET and 2) check $argv, also simple error checking will
> fail for command line stuff when checking that all the required
> params are present. 
> 
> 

ic now i understand what you mean :)
Why not just pass the arguments on commandline like this:

php myscript.php 1=a 2=b

and then just split on '=' to get key & value.

>>> From what i understand the argv & argc are only avaliable when the
>> script is run from commandline. Just do a check on this and if not
>> there get some $_POST or $_GET variables.
>> 
>>>> 
>>>>> 
>>>>> but this does not work, the script executes but key is not
>>>>> available as $_GET["key"] in my script. Any pointers will be much
>>>>> appreciated. 
>>>>> 
>>>>> TIA
>>>>> 
>>>>> Phil.
>>>>> 
>>>>> ---
>>>>> Phil Ewington - Technical Director
>>>>> 
>>>>> 43 Plc - Ashdale House
>>>>> 35 Broad Street, Wokingham
>>>>> Berkshire RG40 1AU
>>>>> 
>>>>> T: +44 (0)1189 789 500
>>>>> F: +44 (0)1189 784 994
>>>>> E: mailto:[EMAIL PROTECTED]
>>>>> W: www.soyouthink.com
>>>>> 
>>>>> ---
>>>>> Outgoing mail is certified Virus Free.
>>>>> Checked by AVG anti-virus system (http://www.grisoft.com).
>>>>> Version: 6.0.701 / Virus Database: 458 - Release Date: 07/06/2004
>>>> 
>>>> ---
>>>> Incoming mail is certified Virus Free.
>>>> Checked by AVG anti-virus system (http://www.grisoft.com).
>>>> Version: 6.0.701 / Virus Database: 458 - Release Date: 07/06/2004
>>>> 
>>> ---
>>> Outgoing mail is certified Virus Free.
>>> Checked by AVG anti-virus system (http://www.grisoft.com).
>>> Version: 6.0.701 / Virus Database: 458 - Release Date: 07/06/2004
>> 
>> ---
>> Incoming mail is certified Virus Free.
>> Checked by AVG anti-virus system (http://www.grisoft.com).
>> Version: 6.0.701 / Virus Database: 458 - Release Date: 07/06/2004
>> 
> ---
> Outgoing mail is certified Virus Free.
> Checked by AVG anti-virus system (http://www.grisoft.com).
> Version: 6.0.701 / Virus Database: 458 - Release Date: 07/06/2004

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to