7

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.

0

11 Answers 11

7

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.

Sign up to request clarification or add additional context in comments.

Comments

4

You can use the getopt() function.

Check blog post PHP CLI script and Command line arguments.

1 Comment

According to this site: tuxradar.com/practicalphp/21/2/4 getopt() doesn't work in Windows (which the OP is using).
3

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.

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, and -e options after the -- are passed to file myScript.php. The ones before are passed to PHP itself).

Comments

3

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

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

Would produce a variable, $test, with a value of <myValue>.

Comments

1

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

You most likely want to use the PEAR 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];
    }
}

Comments

1

Command-line example:

    php myserver.php host=192.168.1.4 port=9000

In file 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'];
    }
?>

1 Comment

This should be the accepted answer, the first line parse_str(implode('&', array_slice($argv, 1)), $_GET); does exactly what is needed - to convert all they key=value pairs into a $_GET[ key => value ] array
0

$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";

Comments

0

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.

Comments

0

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.

Comments

0

You could use something like

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

(How to handle the lack of parameters is up to you.)

Or if you need a more complex scenario, look into a command-line 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 doesn’t really sound safe.

Comments

-1

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 isn't any 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 isn't any need to put a name for the param. Just enter the param value.

1 Comment

print_r($argv) is more useful for troubleshooting an array, imho.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.