Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将shell变量作为参数传递到PHP函数调用中?_Php_Shell_Command Line Interface - Fatal编程技术网

如何将shell变量作为参数传递到PHP函数调用中?

如何将shell变量作为参数传递到PHP函数调用中?,php,shell,command-line-interface,Php,Shell,Command Line Interface,我正在从命令行调用一个PHP脚本: /usr/bin/php -r "require '/path/to/php/dummy.php'; echo getFunction(\"DummyParameter\")"; 调用的工作方式就像一个符咒,PHP函数返回正确的值。我想用$ShellVariable替换DummyParameter。当我替换DummyParameter时,它应该作为“$ShellVariable”字符串传递。以下是我尝试过的: /usr/bin/php -r "require

我正在从命令行调用一个PHP脚本:

/usr/bin/php -r "require '/path/to/php/dummy.php'; echo getFunction(\"DummyParameter\")";
调用的工作方式就像一个符咒,PHP函数返回正确的值。我想用
$ShellVariable
替换
DummyParameter
。当我替换
DummyParameter
时,它应该作为
“$ShellVariable”
字符串传递。以下是我尝试过的:

/usr/bin/php -r "require '/path/to/php/dummy.php'; echo getFunction(\"$ShellVariable\")";
有没有办法将shell变量作为函数参数传递到PHP脚本中


提前感谢。

传递CLI参数并使用数组,例如:

php -r 'printf("%s\n", $argv[1]);' "$(date)"
样本输出


您可能可以,但要获得正确的转义可能非常困难,并且需要相当混乱的代码。这是概念的证明吗?因为如果你只是把代码写在一个文件里,那就很简单了。
Tue Nov 29 14:09:07 +07 2016