使用变量从命令行运行 php 脚本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/974356/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 00:32:21  来源:igfitidea点击:

Run php script from command line with variable

phpcommand-line

提问by Hintswen

I want to run a PHP script from the command line, but I also want to set a variable for that script.

我想从命令行运行 PHP 脚本,但我也想为该脚本设置一个变量。

Browser version: script.php?var=3

浏览器版本: script.php?var=3

Command line: php -f script.php(but how do I give it the variable containing 3?)

命令行:(php -f script.php但我如何给它包含 3 的变量?)

回答by Ionu? G. Stan

Script:

脚本:

<?php

// number of arguments passed to the script
var_dump($argc);

// the arguments as an array. first argument is always the script name
var_dump($argv);

Command:

命令:

$ php -f test.php foo bar baz
int(4)
array(4) {
  [0]=>
  string(8) "test.php"
  [1]=>
  string(3) "foo"
  [2]=>
  string(3) "bar"
  [3]=>
  string(3) "baz"
}

Also, take a look at using PHP from the command line.

另外,看看从命令行使用 PHP

回答by Matthew Flaschen

Besides argv (as Ionut mentioned), you can use environment variables:

除了 argv(正如 Ionut 提到的),您还可以使用环境变量:

E.g.:

例如:

var = 3 php -f test.php

In test.php:

在 test.php 中:

$var = getenv("var");

回答by VolkerK

If you want to keep named parameters almost like var=3&foo=bar (instead of the positional parameters offered by $argv) getopt()can assist you.

如果你想保持命名参数几乎像 var=3&foo=bar (而不是$argv提供的位置参数)getopt()可以帮助你。

回答by Paul Dixon

As well as using argcand argvas indicated by Ionut G. Stan, you could also use the PEAR module Console_Getoptwhich can parse out unix-style command line options. See this articlefor more information.

除了使用Ionut G. Stan指出的argcargv 之外,您还可以使用 PEAR 模块Console_Getopt,它可以解析出 unix 样式的命令行选项。有关更多信息,请参阅此文章

Alternatively, there's similar functionality in the Zend Framework in the Zend_Console_Getoptclass.

或者,在Zend_Console_Getopt类中的 Zend 框架中也有类似的功能。

回答by atmelino

A lot of solutions put the arguments into variables according to their order. For example,

许多解决方案根据参数将参数放入变量中。例如,

myfile.php 5 7

will put the 5 into the first variable and 7 into the next variable.

将 5 放入第一个变量,将 7 放入下一个变量。

I wanted named arguments:

我想要命名参数:

myfile.php  a=1 x=8

so that I can use them as variable names in the PHP code.

这样我就可以在 PHP 代码中将它们用作变量名。

The link that Ionu? G. Stan gave at http://www.php.net/manual/en/features.commandline.php

Ionu 的链接?G. Stan 在 http://www.php.net/manual/en/features.commandline.php 给出

gave me the answer.

给了我答案。

sep16 at psu dot edu:

sep16 在 psu dot edu:

You can easily parse command line arguments into the $_GET variable by using the parse_str() function.

您可以使用 parse_str() 函数轻松地将命令行参数解析为 $_GET 变量。

<?php
parse_str(implode('&', array_slice($argv, 1)), $_GET);
?>

It behaves exactly like you'd expect with cgi-php.

它的行为与您对 cgi-php 的期望完全一样。

$ php -f somefile.php a=1 b[]=2 b[]=3

This will set $_GET['a'] to '1' and $_GET['b'] to array('2', '3').

这会将 $_GET['a'] 设置为 '1',并将 $_GET['b'] 设置为 array('2', '3')。