在Linux系统通过命令行执行php文件怎么传参呢?估计很多朋友知道如何来执行php 但是不知道如何把参数传给文件了,具体的操作步骤如下.
要做多语言搜索索引,可是php程序要通过在Linux系统的php命令来执行文件,这样php程序文件中的变量便无法获取.
所以要进行传参,但是命令行下执行php,是不走apache等这类东西的,没有什么http协议,所有get,post传参根本不起作用,并且会被报错,如下:
root@test:/pub/www/# php /pub/www/product/Crondo/makeindex.php?lang=en
Could not open input file: /pub/www/product/Crondo/makeindex.php?lang=en
可以通过在php执行文件中获取系统环境变量$argv 来得到要传递的参数.
1.test.php测试文件:
#!/usr/bin/php
<?php
print_r($argv);
echo “\n”;
echo “Total number of paramter is : “.$argc;
echo “\n”;
?>
2.命令行调用:
test@ubuntu:~/php$ php test.php 111 222 333 444
All param is :
Array
(
[0] => test.php //参数0,脚本本身的文件名
[1] => 111//参数1
[2] => 222//参数2
[3] => 333//参数3
[4] => 444//参数4
) //phpfensi.com
Total number of paramter is : 5 //$argc的值为参数的总数
这种传参方式,跟shell很像.
test@ubuntu:~/shell$ sh test.sh 111 222
向test.sh传了两个参数111,222,shell会得到三个参数,$0为文件本身,$1为参数1,$2为参数2.不同的是,php得到的是以数组的形式,而shell不是.
大型站长资讯类网站! https://www.nzzz.com.cn