PHP, pass parameters from command line to a PHP script

半腔热情 提交于 2019-11-28 04:32:36
vascowhite

When calling a PHP script from the command line you can use $argc to find out how many parameters are passed and $argv to access them. For example running the following script:

<?php
    var_dump($argc); //number of arguments passed 
    var_dump($argv); //the arguments passed
?>

Like this:-

php script.php arg1 arg2 arg3

Will give the following output

int(4)
array(4) {
  [0]=>
  string(21) "d:\Scripts\script.php"
  [1]=>
  string(4) "arg1"
  [2]=>
  string(4) "arg2"
  [3]=>
  string(4) "arg3"
}

See $argv and $argc for further details.

To do what you want, lets say

php script.php arg1=4

You would need to explode the argument on the equals sign:-

list($key, $val) = explode('=', $argv[1]);
var_dump(array($key=>$val));

That way you can have whatever you want in front of the equals sign without having to parse it, just check the key=>value pairs are correct. However, that is all a bit of a waste, just instruct the user on the correct order to pass the arguments.

While the answer is correct and you could do the parsing by hand, PHP also offers the getopt() function that might actually provide useful here.

There's also object-oriented alternatives (written in PHP, available in a number of libraries) that might turn out to be what you need. Googling for "php getopt" will yield helpful results.

I use this fairly concise method:

if($argc>1)
  parse_str(implode('&',array_slice($argv, 1)), $_GET);

Which would handle a call such as:

php script.php item1=4 item2=300

By sending it into $_GET you automatically handle web or CLI access.

For commentary, this is doing the following:

  • If the count of arguments is greater than one (as first item is the name of the script) the proceed
  • Grab the arguments array excluding first item
  • Turn it into a standard query string format with ampersands
  • use parse_str to extract to the $_GET array
bnp887

The getopt() function is probably the most correct answer in the case of the question. Especially since it was made platform independent with PHP 5.3. In the particular case of this question and parsing multiple parameters, one way to leverage this function would be as follows:

$defaultValues = array("inputFirstName" => "");
$givenArguments = getopt("", array("inputFirstName:"));
$options = array_merge($defaultValues, $givenArguments);
$inputFirstName = $options['inputFirstName'];

The call to set $inputFirstName with the value "Robby" would be:

> php script.php --inputFirstName="Robby"

Explanation

Default values for all expected parameters are set in the $defaultValues array. Input sent through via command line arguments are collected by PHP's getopt function and stored by the $givenArguments. Note that the colon (:) at the end of the "inputFirstName:" string indicates that this is a required argument. Without a colon here, only the presence of the argument would be detected, not the actual value (more information in the PHP Manual). Merging these two arrays together on the third line results in array with all expected parameters being set with either default values or arguments provided from the command line/terminal if they are available.

If you don't mind using a library, I suggest you take a look at The Console Component by Symfony.

It can be used to create command line applications and supports the use of Arguments & Options.

The documentation page contains a couple of excellent examples to get you started.

Of course under the hood it uses the same techniques as explained by vascowhite.

You can parse the user input on your program looking for specific strings such as -inputFirstName="x" (using regular expressions, for example) and then set the correct variable to x.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!