views:

5604

answers:

8

I'm trying to run PHP from the command line under Windows XP.

That works, except for the fact that I am not able to provide parameters to my PHP script.

My test case:

echo "param = ".$param."\n";  
var_dump($argv);

I want to call this as:

php.exe -f test.php -- param=test

But I never get the script to accept my parameter.

The result I get from the above script

`PHP Notice: Undefined variable: param in C:\test.php on line 2

param = ''
array(2) {
  [0]=> string(8) "test.php"
  [1]=> string(10) "param=test"
}

I am trying this using PHP 5.2.6. Is this a bug in PHP5?

The parameter passing is handled in the online help Note: If you need to pass arguments to your scripts you need to pass -- as the first argument when using the -f switch. This seemed to be working under PHP4, but not under PHP5. Under PHP4 I could use the same script that could run on the server without alteration on the command line. This is handy for local debugging, for example saving the output in a file to be studied.

+2  A: 

Why do you have any expectation that param will be set to the value? You're responsible for parsing the command line in the fashion you desire, from the $argv array.

Adam Wright
A: 

$argv is an array containing all your commandline parameters... You need to parse that array and set $param yourself.

$tmp = $argv[1];             // $tmp="param=test"
$tmp = explode("=", $tmp);   // $tmp=Array( 0 => param, 1 => test)

$param = $tmp[1];            // $param = "test";
Ben
+1  A: 

PHP does not parameterize your command line parameters for you. See the output where your 2nd entry in ARGV is "param=test".

What you most likely want is to use the PEAR package http://pear.php.net/package/Console_CommandLine: "A full featured command line options and arguments parser".

Or you can be masochistic and add code to go through your ARGV and set the parameters yourself. Here's a very simplistic snippet to get you started (this won't work if the first part isn't a valid variable name or there is more than 1 '=' in an ARGV part:

foreach($argv as $v) {
    if(false !== strpos($v, '=')) {
     $parts = explode('=', $v);
     ${$parts[0]} = $parts[1];
    }
}
Jeremy Weathers
A: 

You can do something like:

if($argc > 1){
    if($argv[1] == 'param=test'){
     $param = 'test';
    }
}

Of course, you can get much more complicated than that as needed.

Derek Kurth
A: 

The parameter passing is handled in the online help Note: If you need to pass arguments to your scripts you need to pass -- as the first argument when using the -f switch. This seemed to be working under PHP4, but not under PHP5.

But PHP still doesn't parse those arguments. It just passes them to the script in the $argv array.

The only reason for the -- is so that PHP can tell which arguments are meant for the PHP executable and which arguments are meant for your script.

That lets you do things like this:

php -e -n -f myScript.php -- -f -n -e

(The -f, -n, & -e after the -- are passed to myScript.php. The ones before are passed to PHP itself).

Mark Biek
A: 

you can use the $argv array. like this:

<?php
 echo  $argv[1];
?>

remember that the first member of the $argv array (which is $argv[0]) is the name of the script itself, so in order to use the parameters for the application, you should start using members of the $argv[] from the '1'th index. when calling the application, use this syntax:

php myscript.php -- myValue

there is no need to put a name for the parameter. as you saw, what you called the var_dump() on the $argv[], the second member (which is the first parameter) was the string PARAM=TEST. right? so there is no need to put a name for the param. just enter the param value.

farzad
A: 

You could use something like

if (isset($argv[1]) {
 $arg1 = $argv[1];             
 $arg1 = explode("=", $arg1);   
 $param = $arg1[1];            
}

(how to handle the lack of parameter/s is up to you) or if you need a more complex scenario, look into a commandline parser library such as the one from Pear.

using the ${$parts[0]} = $parts[1]; posted in another solution lets you override any variable in your code, which doesnt really sound safe.

rcphq
A: 

If you like living on the cutting edge, PHP 5.3 has the getOpt() command which will take care of all this messy business for you. Somewhat.

kander