运行 PHP 来自 CLI 的脚本并传递参数
Run PHP script from CLI and pass parameters
我有一个 PHP 脚本 check.php 有一个函数
<?php
function checkValues($arg1, $arg2){
...
}
?>
如何通过 Linux 命令行和 运行 脚本将参数传递给函数?
当运行来自命令行的脚本时,所有参数都在文件名之后传入,例如php my-script.php 123
将在名为 $argv
的 PHP 数组中。
array(2) {
[0]=> string(13) "my-script.php"
[1]=> int(3) 123
}
你需要运行 PHP:
php /my/script.php arg1 arg2
然后在您的 PHP 文件中:
<?php
// It's 0 based, but arg1 will be at index 1!
var_dump($argv);
我有一个 PHP 脚本 check.php 有一个函数
<?php
function checkValues($arg1, $arg2){
...
}
?>
如何通过 Linux 命令行和 运行 脚本将参数传递给函数?
当运行来自命令行的脚本时,所有参数都在文件名之后传入,例如php my-script.php 123
将在名为 $argv
的 PHP 数组中。
array(2) {
[0]=> string(13) "my-script.php"
[1]=> int(3) 123
}
你需要运行 PHP:
php /my/script.php arg1 arg2
然后在您的 PHP 文件中:
<?php
// It's 0 based, but arg1 will be at index 1!
var_dump($argv);