PHP - command line arguments in Windows

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

This works, except for the fact that I cannot provide parameters for my PHP script.

My test case:

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

I want to call it like:

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

But I never get a script to accept my parameter.

The result I got from the script

`PHP Note: 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 to use PHP 5.2.6. Is this a bug in PHP5?

Passing parameters 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 work under PHP4, but not under PHP5. In PHP4, I could use the same script that could work on the server without changing the command line. This is convenient for local debugging, for example, to save the output in a file that you need to examine.

+7
command-line php parameters
source share
11 answers

Why do you have the expectation that the parameter will be set to a value? You are responsible for parsing the command line from the $ argv array.

+7
source share

Passing parameters is processed 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 work under PHP4, but not under PHP5.

But PHP still does not parse these arguments. It just passes them to the script in the $ argv array.

The only reason for this is because PHP can determine which arguments are for the PHP executable and which arguments are for your script.

This allows you to do things like this:

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

(The -f, -n and -e after ) are passed to myScript.php. Those that used to be passed to PHP itself).

+3
source share

If you want to pass parameters similar to GET vars, then you can use the parse_str () function. Something like this:

 <?php parse_str($argv[1]); ?> 

Creates a $ test variable with the value myValue.

Hope this helps!

+3
source share
+2
source share

PHP does not parameterize your command line options for you. See Conclusion, where your second entry in ARGV is "param = test".

Most likely, you want to use the PEAR package http://pear.php.net/package/Console_CommandLine : "Full-featured command-line options and argument parser."

Or you can be a masochist and add code to go through your ARGV and set the parameters yourself. Here's a very simplified snippet to get you started (this will not work if the first part is not a valid variable name or in the ARGV part is greater than 1 '=':

 foreach($argv as $v) { if(false !== strpos($v, '=')) { $parts = explode('=', $v); ${$parts[0]} = $parts[1]; } } 
+1
source share

$ argv is an array containing all the parameters of your command line ... You need to parse this array and install $ param yourself.

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

You can do something like:

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

Of course, you can be much more complicated than necessary.

0
source share

You can use something like

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

(how to deal with the lack of the / s option is up to you) or if you need a more complex script, take a look at the command line parsing library, for example, from Pear .

using ${$parts[0]} = $parts[1]; , published in another solution, allows you to override any variable in the code that is really not safe .

0
source share

If you enjoy living at the forefront, PHP 5.3 has a getOpt () command that takes care of all this messy business for you. Several.

0
source share

command line example:

php myserver.php host = 192.168.1.4 port = 9000

in myserver.php use the following lines:

 <?php parse_str(implode('&', array_slice($argv, 1)), $_GET); // Read Arguments if (array_key_exists('host',$_GET)) { $host = $_GET['host']; } if (array_key_exists('port',$_GET)) { $port = $_GET['port']; } ?> 
0
source share

you can use the $ argv array. eg:

 <?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 must start using the $ argv [] members from the '1st index. when invoking the application, use this syntax:

 php myscript.php -- myValue 

no need to specify a name for the parameter. as you saw, what you called var_dump () on $ argv [], the second element (which is the first parameter) is the line PARAM = TEST. correctly? therefore, there is no need to specify a name for the parameter. just enter the parameter value.

-one
source share

All Articles