命令行选项
PHP 二进制文件可以随时通过执行带 -h 参数的 PHP 命令获取提供的命令行选项列表:
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>.
| 选项 | 长选项 | 说明 | 
|---|---|---|
| -a | --interactive | 
         运行交互式 PHP。参阅交互式 shell 章节获取更多信息。  | 
      
| -b | --bindpath | 
         外部 FASTCGI 服务器模式绑定路径(仅 CGI 可用)。  | 
      
| -C | --no-chdir | 
         不改变脚本的目录(仅 CGI 可用)。  | 
      
| -q | --no-header | 
         安静模式。禁止输出 HTTP 头(仅 CGI 可用)。  | 
      
| -T | --timing | 
         测量脚本重复 count 次的执行时间(仅 CGI 可用)。  | 
      
| -c | --php-ini | 
         
         在指定目录查找 php.ini 或者自定义  $ php -c /custom/directory/ my_script.php $ php -c /custom/directory/custom-file.ini my_script.php 如果未指定此选项,php.ini 将在默认位置搜索。  | 
      
| -n | --no-php-ini | 
         完全忽略 php.ini。  | 
      
| -d | --define | 
         允许设置 php.ini 中配置指令的值。语法是: -d configuration_directive[=value] 示例 #1 使用  
# 忽略值部分,将会设置配置指令为 "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=doesntmakesense
        -r '$foo = ini_get("max_execution_time"); var_dump($foo);'
string(15) "doesntmakesense"
 | 
      
| -e | --profile-info | 
         激活扩展信息模式,用于调试/分析。  | 
      
| -f | --file | 
         解析并执行指定文件。 -f 可选且可以忽略 —— 只需提供需要执行的文件名就足够。  | 
      
| -h and -? | --help and --usage | 输出命令行选项列表并描述了这些选项的作用。 | 
| -i | --info | 调用 phpinfo() 并输出结果。如果 PHP 不能正常工作,建议使用命令 php -i 查看在信息表输出之前或者中间某个位置是否有错误消息。注意当使用 CGI 模式时会输出 HTML, 这会非常大。 | 
| -l | --syntax-check | 
         
         检查语法但不执行指定 PHP 代码。如果未指定文件,则将处理来自标准输入的输入,否则将检查每个文件。成功时,文本
          此选项不会找到需要执行代码的致命错误(如未定义的函数)。 
 
  | 
      
| -m | --modules | 
         示例 #2 打印内置(且已加载的) PHP 和 Zend 模块 $ php -m [PHP Modules] xml tokenizer standard session posix pcre overload mysql mbstring ctype [Zend Modules]  | 
      
| -r | --run | 
         
         允许在命令行内直接执行单行 PHP 代码。
         不需要加上 PHP 开始和结束标识符( 
 
 
  | 
      
| -B | --process-begin | 
         处理 stdin 之前需要执行的 PHP 代码。  | 
      
| -R | --process-code | 
         对每个输入行都执行的 PHP 代码。 此模式下有两个特殊变量: $argn 和 $argi。 $argn 将包含 PHP 正在处理的行, $argi 将包含正在处理的行号。  | 
      
| -F | --process-file | 
         对每个输入行都执行的 PHP 文件。  | 
      
| -E | --process-end | 
         在处理完输入后执行的 PHP 代码。 示例 #5 使用 -B 、 -R 、 -E 选项统计项目总行数。 $ find my_proj | php -B '$l=0;' -R '$l += count(@file($argn));' -E 'echo "Total Lines: $l\n";' Total Lines: 37328  | 
      
| -S | --server | 
         启动 内置 web 服务器.  | 
      
| -t | --docroot | 为内置 web 服务器指定文档根目录。 | 
| -s | --syntax-highlight 和 --syntax-highlighting | 
         为源代码添加语法高亮显示。 
         此选项将使用内部机制解析文件并将生成的 HTML 高亮版本写入到标准输出。
         注意它所做的只是生成一块  
  | 
      
| -v | --version | 
         示例 #6 使用 -v 获取 SAPI 的名称以及 PHP 和 Zend 的版本号 $ php -v PHP 5.3.1 (cli) (built: Dec 11 2009 19:55:07) Copyright (c) 1997-2009 The PHP Group Zend Engine v2.3.0, Copyright (c) 1998-2009 Zend Technologies  | 
      
| -w | --strip | 
         显示忽略注释和空格后的源代码。 
  | 
      
| -z | --zend-extension | 
         加载 Zend 扩展。如果仅指定了文件名,PHP 将尝试从当前系统默认函数库中尝试加载此扩展 (例如在 Linux 上通常是 /etc/ld.so.conf)。 传递绝对路径的文件名将不会使用系统库搜索路径。如果用相对路径指定的文件名,则 PHP 仅试图在当前目录的相对目录加载扩展库。  | 
      
| --ini | 
         展示配置文件名和扫描目录。 示例 #7  $ php --ini Configuration File (php.ini) Path: /usr/dev/php/5.2/lib Loaded Configuration File: /usr/dev/php/5.2/lib/php.ini Scan for additional .ini files in: (none) Additional .ini files parsed: (none)  | 
      |
| --rf | --rfunction | 
         展示指定函数或者类方法的有关信息(例如参数名称和数量)。 如果 PHP 在编译时启用 Reflection 支持,该选项才可以使用。 
 示例 #8 基础  $ php --rf var_dump
Function [ <internal> public function var_dump ] {
  - Parameters [2] {
    Parameter #0 [ <required> $var ]
    Parameter #1 [ <optional> $... ]
  }
}
 | 
      
| --rc | --rclass | 
         展示指定类的有关信息(常量、属性和方法的列表)。 如果 PHP 在编译时启用 Reflection 支持,该选项才可以使用。 
 示例 #9  $ php --rc Directory
Class [ <internal:standard> class Directory ] {
  - Constants [0] {
  }
  - Static properties [0] {
  }
  - Static methods [0] {
  }
  - Properties [0] {
  }
  - Methods [3] {
    Method [ <internal> public method close ] {
    }
    Method [ <internal> public method rewind ] {
    }
    Method [ <internal> public method read ] {
    }
  }
}
 | 
      
| --re | --rextension | 
         展示指定扩展的有关信息(php.ini 选项、定义函数、常量和类的列表)。 如果 PHP 在编译时启用 Reflection 支持,该选项才可以使用。 
 示例 #10  $ php --re json
Extension [ <persistent> extension #19 json version 1.2.1 ] {
  - Functions {
    Function [ <internal> function json_encode ] {
    }
    Function [ <internal> function json_decode ] {
    }
  }
}
 | 
      
| --rz | --rzendextension | 
         展示指定 Zend 扩展的配置信息(也可以通过 phpinfo() 返回相同信息)。  | 
      
| --ri | --rextinfo | 
         展示指定扩展的配置信息(也可以通过 phpinfo() 返回相同信息)。 使用 “main” 作为扩展名可以获取到核心配置信息。 
 示例 #11  $ php --ri date date date/time support => enabled "Olson" Timezone Database Version => 2009.20 Timezone Database => internal Default timezone => Europe/Oslo Directive => Local Value => Master Value date.timezone => Europe/Oslo => Europe/Oslo date.default_latitude => 59.930972 => 59.930972 date.default_longitude => 10.776699 => 10.776699 date.sunset_zenith => 90.583333 => 90.583333 date.sunrise_zenith => 90.583333 => 90.583333  | 
      
注意:
选项
-rBRFEH、--ini、--r[fcezi]仅可以在 CLI 中使用。
用户贡献的备注 2 notes
If you would like to see the PHP's current configuration using the -i switch from the shell command line with php.ini specified, then the order of arguments is important. Putting -i after -c gives the intended result.
Info is printed out for the default php.ini (cli)
$ php -i --php-ini /etc/php/7.4/fpm/php.ini | grep -i "loaded conf"
Loaded Configuration File => /etc/php/7.4/cli/php.ini
Info is printed out for the desired php.ini (fpm)
$ php --php-ini /etc/php/7.4/fpm/php.ini -i | grep -i "loaded conf"
Loaded Configuration File => /etc/php/7.4/fpm/php.iniIf we start the php's built in webserver (PHP v5.4 onwards) with:
        php -S localhost:8000 -t htdocs
and have an image file picture.jpg in it
and reference it in a html page with:
         <img src="picture.jpg">
the rendered page will not show the image and the html code behind the image is:
http://localhost:8000/index.php/picture.jpg
If however, the html code in the page is:
         <img src="/picture.jpg">
the picture displays correctly.
Hence relative addressing is broken in PHP 5.4.33 Win32 VC9 build.备份地址:http://www.lvesu.com/blog/php/features.commandline.options.php