使用shell向php传递参数
我的问题可能很容易回答。我想用 shell 执行我的 php 文件并通过 shell 将参数传递给它 例如,
php test.php parameter1 parameter2
除了使用 GET 之外,还有其他方法可以做到这一点吗?
谢谢
my question is probably easy to answer. i want to execute my php file with shell and pass parameters to it via shell
example
php test.php parameter1 parameter2
is there a way to do that except using GET ?
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,您可以这样做,但您应该引用
$_SERVER['argv']
数组中的参数。$_SERVER['argc']
将告诉您收到了多少个参数,如果您想将其用作第一层验证以确保输入所需数量的参数。为了说明这一点,运行以下脚本
args.php arg1 arg2 arg3
:将输出:
这是一个实际示例:
在本例中,我们将创建一个脚本 (days.php),输出自特定日期以来的天数。它将接受 3 个参数,即数字形式的月、日和年。
Shell 调用:
输出:
希望这有帮助。
Yes you can do it like that but you should reference the arguments from the
$_SERVER['argv']
array.$_SERVER['argc']
will tell you how many args were received, should you want to use that as a first layer of validation to make sure a required number of args were input.To illustrate this, running the following script as
args.php arg1 arg2 arg3
:will output:
Here is a practical example:
In this example, we'll create a script (days.php) that outputs the number of days since a particular date. It will accept 3 parameters, the month, day, and year as numbers.
Shell call:
Output:
Hope this helps.
您可以使用 $argv 来获取参数。
You can use $argv to get the parameters.