LINUX命令行下运行php且带参数

时间:2022-05-26 04:41:56

在linux中直接用"php"命令来执行php文件

  一般在linux命令行下运行php文件的代码:XML/HTML代码

  linux下执行:#php安装路径 -f php文件路径 例如:/opt/modules/php/bin/php -f /opt/data/www/yoururl/index.php

  每次输入php安装路径比较麻烦,其实我们可以不用输入的哦!

  将/opt/modules/php/bin/php 这个文件复制到/usr/sbin下,php将被安装为linux命令

  在linux下执行以下命令

  XML/HTML代码

  cp /opt/modules/php/bin/php /usr/sbin 这样就可以直接在命令行中用

  XML/HTML代码

  php -f /opt/data/www/yoururl/index.php

  执行php程序了,省去输入路径的过程

  命令行中php接收参数

  命令行中给php文件输入参数和在http协议下方式不一样,不是用变量名来接收,而是用位置处于第几个来接收

  其中,第一个参数用$_SERVER['argv'][1]接收,第二个参数用$_SERVER['argv'][2]接收

  /opt/data/www/yoururl/index.php 中这样写:

  XML/HTML代码

  <?php if (isset($_SERVER['argv'][1])) { $year =$_SERVER['argv'][1]; } else { $year = 'null'; } if(isset($_SERVER['argv'][2])) { $month = $_SERVER['argv'][2]; } else {$month = 'null'; } if (isset($_SERVER['argv'][3])) { $day =$_SERVER['argv'][3]; } else { $day = 'null'; } echo 'date is'.$year.'-'.$month.'-'.$$day; ?> 在命令行中运行:

  XML/HTML代码

  /opt/modules/php/bin/php -f /opt/data/www/yoururl/index.php 2008 10 16

  输出:

  XML/HTML代码

  date is 2008-10-16