现在的位置: 首页 > 综合 > 正文

命令行下运行php的方法和技巧

2013年07月24日 ⁄ 综合 ⁄ 共 889字 ⁄ 字号 评论关闭

 

在linux中直接用"php"命令来执行php文件
一般在linux命令行下运行php文件的代码:

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下执行以下命令

  1. cp /opt/modules/php/bin/php  /usr/sbin

复制代码

这样就可以直接在命令行中用

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

复制代码

执行php程序了,省去输入路径的过程
命令行中php接收参数
命令行中给php文件输入参数和在http协议下方式不一样,不是用变量名来接收,而是用位置处于第几个来接收
其中,第一个参数用$_SERVER['argv'][1]接收,第二个参数用$_SERVER['argv'][2]接收
/opt/data/www/yoururl/index.php 中这样写:
[php]<?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;
?>[/php]
在命令行中运行:

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

复制代码

输出:

date is 2008-10-16

抱歉!评论已关闭.