命令列選項
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"
$ 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 程式碼進行語法檢查的便捷方法。
成功時將會在標準輸出中寫入
此選項不能發現像是函式未定義之類的核心錯誤(fatal errors)。可以使用 -f 測試核心錯誤(fatal errors)。
|
| -m | --modules |
示例 #1 列印內建(且已載入的) 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 程式碼。 示例 #4 使用 -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 |
示例 #5 使用 -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 |
展示配置檔名和掃瞄目錄。 示例 #6 $ 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 支援,該選項才可以使用。
示例 #7 基礎 $ php --rf var_dump
Function [ <internal> public function var_dump ] {
- Parameters [2] {
Parameter #0 [ <required> $var ]
Parameter #1 [ <optional> $... ]
}
}
|
| --rc | --rclass |
展示指定類的有關資訊(常量、屬性和方法的列表)。 如果 PHP 在編譯時啟用 Reflection 支援,該選項才可以使用。
示例 #8 $ 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 支援,該選項才可以使用。
示例 #9 $ 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」 作為副檔名可以獲取到核心配置資訊。
示例 #10 $ 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 中使用。