PHP – command line arguments in Windows

command lineparametersPHP

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 PHP 5?

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 PHP 4, but not under PHP 5.

Under PHP 4 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.

Best Answer

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.