php 命令行执行参数详解

为了方便在任意目录下执行php命令行命令,我们把php.exe所在的路径配置到系统的环境变量path。

执行 php -h 命令我们可以看到如下所示命令参数(可能由于版本不同参数个数会有不同):

Usage: php [options] [-f] <file> [--] [args...]
   php [options] -r <code> [--] [args...]
   php [options] [-B <begin_code>] -R <code> [-E <end_code>] [--] [args...]
   php [options] [-B <begin_code>] -F <file> [-E <end_code>] [--] [args...]
   php [options] -- [args...]
   php [options] -a
  -a               Run interactively
  -c <path>|<file> Look for php.ini file in this directory
  -n               No php.ini file will be used
  -d foo[=bar]     Define INI entry foo with value 'bar'
  -e               Generate extended information for debugger/profiler
  -f <file>        Parse and execute <file>.
  -h               This help
  -i               PHP information
  -l               Syntax check only (lint)
  -m               Show compiled in modules
  -r <code>        Run PHP <code> without using script tags <?..?>
  -B <begin_code>  Run PHP <begin_code> before processing input lines
  -R <code>        Run PHP <code> for every input line
  -F <file>        Parse and execute <file> for every input line
  -E <end_code>    Run PHP <end_code> after processing all input lines
  -H               Hide any passed arguments from external tools.
  -S <addr>:<port> Run with built-in web server.
  -t <docroot>     Specify document root <docroot> for built-in web server.
  -s               Output HTML syntax highlighted source.
  -v               Version number
  -w               Output source with stripped comments and whitespace.
  -z <file>        Load Zend extension <file>.
  args...          Arguments passed to script. Use -- args when first argument
                   starts with - or script is read from stdin
  --ini            Show configuration file names
  --rf <name>      Show information about function <name>.
  --rc <name>      Show information about class <name>.
  --re <name>      Show information about extension <name>.
  --rz <name>      Show information about Zend extension <name>.
  --ri <name>      Show configuration for extension <name>.

详解:

部分参数有两种形式 选项名称如php -a 长名称如php --interactive他们的效果是一样的。

 -a   (--interactive

交互式运行 PHP。如果编译 PHP 时加入了 Readline 扩展(Windows 下不可用),那将会得到一个很好的外壳,包括一个自动完成的功能(例如可以在键入变量名的时候,按下 TAB 键,PHP 会自动完成该变量名)以及命令历史记录,可以用上下键来访问。历史记录存在 ~/.php_history 文件中。


 -c   (--php-ini)

用该参数,可以指定一个放置 php.ini 文件的目录,或者直接指定一个自定义的 INI 文件(其文件名可以不是 php.ini),个人感觉此参数单独使用应该没有什么意义,应该是在执行php文件之前指定一下此次的.ini文件便于一次性切换大量配置。或者启动php内置服务器时用于指定一个php.ini文件。为了证明此用法我们做如下测试

/*
 php_c_test.php 
 1.将php安装目录下的php.ini文件复制出一份并修改文件名放在同目录下(此处改为了test.ini)
 2.test.ini中的display_errors配置项改为off(在php.ini中默认为on,总之就是为了区别两个文件中的同名配置)
 3.编写代码打印此值(php_c_test.php为我编写的测试文件的文件名)
 4.在命令行分别执行(路径不用说当然是按照你配置文件和测试文件的路径来写了)
 (不指定配置文件则为使用默认配置文件) php -f D:\myweb\apache\htdocs\php_c_test.php 
  结果 string(1) "1"
 (指定为修改后的配置文件) php -c D:\myweb\php-5.3.5\test.ini -f D:\myweb\apache\htdocs\php_c_test.php 
  结果 string(0) ""
*/
var_dump(ini_get('display_errors'));


-n (--no-php-ini)  

完全忽略 php.ini。此参数在 PHP 4.3.0 以后有效。


-d (--define) 

用该参数可以自行设置任何可以在 php.ini 文件中设置的配置选项的值

# 取值部分被省略,将会把配置选项设为 "1"
$ php -d max_execution_time -r '$foo = ini_get("max_execution_time"); var_dump($foo);'
string(1) "1"

# 取值部分为空白,将会把配置选项设为 ""
php -d max_execution_time= -r '$foo = ini_get("max_execution_time"); var_dump($foo);'
string(0) ""

# 配置选项将被设置成为任何 '=' 字符之后的值
$  php -d max_execution_time=20 -r '$foo = ini_get("max_execution_time"); var_dump($foo);'
string(2) "20"
$  php -d max_execution_time=doesntmakesens -r '$foo = ini_get("max_execution_time"); var_dump($foo);'
string(15) "doesntmakesense"


-e   (--profile-info)

激活扩展信息模式,被用于调试/测试。(探索中......)


-f    (--file) 

解析并运行 -f 选项给定的文件名。该参数为可选参数,可以省略,仅指明需要运行的文件名即可。

例如:php -f  D:\newwamp\www\command.php  或  php   D:\newwamp\www\command.php


-h 或 -? (--help 或 --usage) 

使用该参数,可以得到完整的命令行参数的列表及这些参数作用的简单描述。(得到命令行参数的使用信息)


-i     (--info)  

该命令行参数会调用 phpinfo() 函数并显示出结果。如果 PHP 没有正常工作,建议执行 php -i 命令来查看在信息表格之前或者对应的地方是否有任何错误信息输出。请注意当使用 CGI 摸索时,输出的内容为 HTML 格式,因此输出的信息篇幅较大。


-l (L的小写形式,这里太坑了跟i的大写很像) (--syntax-check)  

该参数提供了对指定 PHP 代码进行语法检查的方便的方法。如果成功,则向标准输出写入 No syntax errors detected in <filename> 字符串,并且外壳返回值为 0。如果失败,则输出 Errors parsing <filename> 以及内部解析器错误信息到标准输出,同时外壳返回值将别设置为 255。 
该参数将无法检查致命错误(如未定义函数),如果也希望检测致命错误,请使用 -f 参数,但 -f 可省略 
Note: 
该参数不能和 -r 一同使用。 如下测试:

/*
php_l_test.php
(中间的字母为L的小写)
在文件名为 php_I_test.php 的文件中有var_dump打印函数后没有;结束符此时在php命令行执行
php -l D:\myweb\apache\htdocs\php_l_test.php 命令检查其语法错误

Parse error: syntax error, unexpected $end in D:\myweb\apache\htdocs\php_l_test.
php on line 9
Errors parsing D:\myweb\apache\htdocs\php_l_test.php
*/
var_dump(1)


-m  (--modules) 

使用该参数,PHP 将打印出内置以及已加载的 PHP 及 Zend 模块


-r   (--run) 

使用该参数可以在命令行内运行单行 PHP 代码。无需加上 PHP 的起始和结束标识符(<?php 和 ?>),否则将会导致语法解析错误。例如:

php -r "var_dump('hello world');"
string(11) "hello world"

使用这种形式的 PHP 时,应注意避免和外壳环境进行的命令行参数替换相冲突(主要指单引号双引号的使用)


-B   (--process-begin)  

在处理 stdin 之前先执行 PHP 代码。PHP 5 新加。(探索中......)


-R   (--process-code)    

对每个输入行都执行 PHP 代码。PHP 5 新加。 (探索中......)
此模式下有两个特殊变量:$argn 和 $argi。$argn 包含 PHP 当前处理的行内容,而 $argi 则包含该行号。


-F   (--process-file) 

对每个输入行都执行 PHP 文件。PHP 5 新加。(探索中......)


-E   (--process-end) 

在处理完输入后执行的 PHP 代码。PHP 5 新加。(探索中......)



-H     (--hide-args)          

隐藏任何从外部工具传递的参数。(探索中......)


-S <addr>:<port> 

以内置web 服务器运行

PHP 5.4.0起, CLI SAPI 提供了一个内置的Web服务器。
这个内置的Web服务器主要用于本地开发使用,不可用于线上产品环境。
URI请求会被发送到PHP所在的的工作目录(Working Directory)进行处理,除非你使用了-t参数来自定义不同的录。
如果请求未指定执行哪个PHP文件,则默认执行目录内的index.php 或者 index.html。如果这两个文件都不存在,服务器会返回404错误。
当你在命令行启动这个Web Server时,如果指定了一个PHP文件,则这个文件会作为一个“路由”脚本,意味着每次请求都会先执行这个脚本。如果这个脚本返回 FALSE ,那么直接返回请求的文件(例如请求静态文件不作任何处理)。否则会把输出返回到浏览器。

例如:

php -S localhost:9090

返回如下信息,如果报错说明端口被占用了等
PHP 5.4.16 Development Server started at Tue Sep 27 12:40:35 2016
Listening on http://localhost:9090
Document root is C:\Users\Administrator
Press Ctrl-C to quit.


-t <docroot>     

启动时为内置web服务器指定文档根目录

例如:

php -S localhost:9090 -t D:\newwamp\www
PHP 5.4.16 Development Server started at Tue Sep 27 12:48:30 2016
Listening on http://localhost:9090
Document root is D:\newwamp\www
Press Ctrl-C to quit.


使用路由(注意即使路由文件找不到也不会报错)

php -S localhost:9090 router.php   


既指定目录又使用路由

 php -S localhost:9090 -t D:\newwamp\www  D:\newwamp\www\router.php

测试如下:

php -S localhost:9090 -t D:\www
PHP 5.4.16 Development Server started at Tue Sep 27 13:04:51 2016
Listening on http://localhost:9090
Document root is D:\www
Press Ctrl-C to quit.

然后在D:\www 目录下建立index.php文件,在浏览器地址栏访问  http://localhost:9090/index.php

命令行终端会收到请求,显示如下 而且浏览器也可以看到文件的内容
[Tue Sep 27 13:05:02 2016] ::1:59426 [200]: /index.php


-s (--syntax-highlight)   

显示有语法高亮色彩的源代码。例如:

php -s D:\myweb\apache\htdocs\php_s_test.php (结果由于屏幕显示问题未予复制)


-v (--version)

将 PHP,PHP SAPI 和 Zend 的版本信息写入标准输出。


-w   (--strip) 

显示除去了注释和多余空白的源代码。 
Note: 该选项不能和 -r 参数同时使用。

例如:php -w D:\myweb\apache\htdocs\php_w_test.php

<?php
/* php_w_test.php 这是一个数组*/
$arr = array(
 'a'=>1,
 'b'=>2,
 'c'=>3,
 'd'=>4,
 'e'=>5,
);

输出:
<?php
 $arr = array( 'a'=>1, 'b'=>2, 'c'=>3, 'd'=>4, 'e'=>5, );

-z   (--zend-extension)  

加载 Zend 扩展库。如果仅给定一个文件名,PHP 将试图从当前系统扩展库的默认路径(在 Linux 系统下,该路径通常由 /etc/ld.so.conf 指定)加载该扩展库。如果用一个绝对路径指定文件名,则不会使用系统的扩展库默认路径。如果用相对路径指定的文件名,则 PHP 仅试图在当前目录的相对目录加载扩展库。 
例如:(待完善)


args...        

可以用  --   args1  args2 的形式给脚本传参数(探索中......)


-g name     (--global name) 

设置变量为全局变量



--ini     

 显示配置文件名 

用法  php --ini     


 --rf <name>    (--rfunction <name>)  

显示函数信息

例如:php --rf var_dump

输出:
Function [ <internal:standard> function var_dump ] {


  - Parameters [2] {
    Parameter #0 [ <required> $var ]
    Parameter #1 [ <optional> $... ]
  }
}



 --rc <name>     (--rclass <name>

显示类信息(php内部类)

例如:php --rc PDO
Class [ <internal:PDO> class PDO ] {


  - Constants [76] {
    Constant [ integer PARAM_BOOL ] { 5 }
    Constant [ integer PARAM_NULL ] { 0 }
    Constant [ integer PARAM_INT ] { 1 }
    Constant [ integer PARAM_STR ] { 2 }
    Constant [ integer PARAM_LOB ] { 3 }
    Constant [ integer PARAM_STMT ] { 4 }
    Constant [ integer PARAM_INPUT_OUTPUT ] { -2147483648 }
    Constant [ integer PARAM_EVT_ALLOC ] { 0 }......


 --re <name>    (--rextension <name>)   

显示扩展信息(扩展必须是开启的才可以用此命令查看)

例如:php --re curl
Extension [ <persistent> extension #32 curl version <no_version> ] {
  - Constants [267] {
    Constant [ integer CURLOPT_IPRESOLVE ] { 113 }
    Constant [ integer CURL_IPRESOLVE_WHATEVER ] { 0 }
    Constant [ integer CURL_IPRESOLVE_V4 ] { 1 }
    Constant [ integer CURL_IPRESOLVE_V6 ] { 2 }......


 --ri <name>      (--rextinfo <name>)

显示扩展的配置信息

例如:php --ri curl
curl
cURL support => enabled
cURL Information => 7.20.0
Age => 3
Features
AsynchDNS => Yes
Debug => No
GSS-Negotiate => No
IDN => No ......


研究了两天,发现单纯研究php命令行是不够的,所以需要去研究一些命令行操作再回头完善php命令行。未完待续...




  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

梧桐深院

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值