命令行選項(xiàng)

PHP 二進(jìn)制文件可以隨時(shí)通過執(zhí)行帶 -h 參數(shù)的 PHP 命令獲取提供的命令行選項(xiàng)列表:

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>.

命令行選項(xiàng)
選項(xiàng) 長選項(xiàng) 說明
-a --interactive

運(yùn)行交互式 PHP。參閱交互式 shell 章節(jié)獲取更多信息。

-b --bindpath

外部 FASTCGI 服務(wù)器模式綁定路徑(僅 CGI 可用)。

-C --no-chdir

不改變腳本的目錄(僅 CGI 可用)。

-q --no-header

安靜模式。禁止輸出 HTTP 頭(僅 CGI 可用)。

-T --timing

測(cè)量腳本重復(fù) count 次的執(zhí)行時(shí)間(僅 CGI 可用)。

-c --php-ini

在指定目錄查找 php.ini 或者自定義 INI 文件(名稱可以不是 php.ini),例如:

$ php -c /custom/directory/ my_script.php

$ php -c /custom/directory/custom-file.ini my_script.php

如果未指定此選項(xiàng),php.ini 將在默認(rèn)位置搜索。

-n --no-php-ini

完全忽略 php.ini。

-d --define

允許設(shè)置 php.ini 中配置指令的值。語法是:

 -d configuration_directive[=value]
 

# 忽略值部分,將會(huì)設(shè)置配置指令為 "1"
$ php -d max_execution_time
        -r '$foo = ini_get("max_execution_time"); var_dump($foo);'
string(1) "1"

# 傳遞空值,將會(huì)設(shè)置配置指令為 ""
php -d max_execution_time=
        -r '$foo = ini_get("max_execution_time"); var_dump($foo);'
string(0) ""

# 配置指令將會(huì)設(shè)置為 '=' 字符之后傳遞的任何值
$  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

激活擴(kuò)展信息模式,用于調(diào)試/分析。

-f --file

解析并執(zhí)行指定文件。 -f 可選且可以忽略 —— 只需提供需要執(zhí)行的文件名就足夠。

-h and -? --help and --usage 輸出命令行選項(xiàng)列表并描述了這些選項(xiàng)的作用。
-i --info 調(diào)用 phpinfo() 并輸出結(jié)果。如果 PHP 不能正常工作,建議使用命令 php -i 查看在信息表輸出之前或者中間某個(gè)位置是否有錯(cuò)誤消息。注意當(dāng)使用 CGI 模式時(shí)會(huì)輸出 HTML, 這會(huì)非常大。
-l --syntax-check

提供了僅對(duì)指定 PHP 代碼進(jìn)行語法檢查的便捷方法。 成功時(shí)將會(huì)在標(biāo)準(zhǔn)輸出中寫入 No syntax errors detected in <filename> 并且 shell 返回碼為 0。失敗時(shí)除了將會(huì)在標(biāo)準(zhǔn)輸出中寫入內(nèi)部解析錯(cuò)誤消息之外,還會(huì)寫入 Errors parsing <filename>,并且 shell 返回碼為 -1。

此選項(xiàng)不能發(fā)現(xiàn)像是函數(shù)未定義之類的核心錯(cuò)誤(fatal errors)??梢允褂? -f 測(cè)試核心錯(cuò)誤(fatal errors)。

注意:

此選項(xiàng)不能和 -r 一起使用。

-m --modules

示例 #1 打印內(nèi)置(且已加載的) PHP 和 Zend 模塊

$ php -m
[PHP Modules]
xml
tokenizer
standard
session
posix
pcre
overload
mysql
mbstring
ctype

[Zend Modules]

-r --run

允許在命令行內(nèi)直接執(zhí)行單行 PHP 代碼。 不需要加上 PHP 開始和結(jié)束標(biāo)識(shí)符(<?php?>),否則將會(huì)造成語法錯(cuò)誤。

注意:

使用這種形式的 PHP 時(shí),應(yīng)注意避免和 shell 環(huán)境進(jìn)行的命令行參數(shù)替換相沖突。

示例 #2 當(dāng)使用雙引號(hào)時(shí)會(huì)造成語法錯(cuò)誤

$ php -r "$foo = get_defined_constants();"
PHP Parse error:  syntax error, unexpected '=' in Command line code on line 1

Parse error: syntax error, unexpected '=' in Command line code on line 1

這里的問題是在使用雙引號(hào) " 時(shí) sh/bash 會(huì)執(zhí)行變量替換。 由于 $foo 變量沒有被定義,因此它將替換為空字符, 從而導(dǎo)致傳遞給 PHP 執(zhí)行的代碼實(shí)際上會(huì)變成:

$ php -r " = get_defined_constants();"

正確的方式是使用單引號(hào) ' 。使用單引號(hào)時(shí) sh/bash 不會(huì)進(jìn)行變量替換。

示例 #3 使用單引號(hào)防止 shell 變量替換

$ php -r '$foo = get_defined_constants(); var_dump($foo);'
array(370) {
  ["E_ERROR"]=>
  int(1)
  ["E_WARNING"]=>
  int(2)
  ["E_PARSE"]=>
  int(4)
  ["E_NOTICE"]=>
  int(8)
  ["E_CORE_ERROR"]=>
  [...]

如果使用的 shell 不是 sh/bash,可能會(huì)碰到更多的問題 —— 如果有的話,請(qǐng)向 ? https://github.com/php/php-src/issues 報(bào)告 bug。試圖在命令行中使用( shell/PHP )變量或者使用反斜杠轉(zhuǎn)義時(shí)需要格外小心,因?yàn)楹苋菀子龅絾栴}。勿謂言之不預(yù)!

注意:

可以在 CLI SAPI 中使用 -r,但不能在 CGI SAPI 中使用。

注意:

此選項(xiàng)僅適用于非?;镜拇a,因此在此模式下會(huì)忽略某些配置指令(如 auto_prepend_fileauto_append_file)。

-B --process-begin

處理 stdin 之前需要執(zhí)行的 PHP 代碼。

-R --process-code

對(duì)每個(gè)輸入行都執(zhí)行的 PHP 代碼。

此模式下有兩個(gè)特殊變量: $argn$argi。 $argn 將包含 PHP 正在處理的行, $argi 將包含正在處理的行號(hào)。

-F --process-file

對(duì)每個(gè)輸入行都執(zhí)行的 PHP 文件。

-E --process-end

在處理完輸入后執(zhí)行的 PHP 代碼。

示例 #4 使用 -B 、 -R-E 選項(xiàng)統(tǒng)計(jì)項(xiàng)目總行數(shù)。

$ find my_proj | php -B '$l=0;' -R '$l += count(@file($argn));' -E 'echo "Total Lines: $l\n";'
Total Lines: 37328

-S --server

啟動(dòng) 內(nèi)置 web 服務(wù)器.

-t --docroot 內(nèi)置 web 服務(wù)器指定文檔根目錄。
-s --syntax-highlight 和 --syntax-highlighting

為源代碼添加語法高亮顯示。

此選項(xiàng)將使用內(nèi)部機(jī)制解析文件并將生成的 HTML 高亮版本寫入到標(biāo)準(zhǔn)輸出。 注意它所做的只是生成一塊 <code> [...] </code> HTML 標(biāo)記,沒有 HTML header。

注意:

此選項(xiàng)不能和 -r 一起使用。

-v --version

示例 #5 使用 -v 獲取 SAPI 的名稱以及 PHP 和 Zend 的版本號(hào)

$ 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

顯示忽略注釋和空格后的源代碼。

注意:

此選項(xiàng)不能和 -r 一起使用。

-z --zend-extension

加載 Zend 擴(kuò)展。如果僅指定了文件名,PHP 將嘗試從當(dāng)前系統(tǒng)默認(rèn)函數(shù)庫中嘗試加載此擴(kuò)展 (例如在 Linux 上通常是 /etc/ld.so.conf)。 傳遞絕對(duì)路徑的文件名將不會(huì)使用系統(tǒng)庫搜索路徑。如果用相對(duì)路徑指定的文件名,則 PHP 僅試圖在當(dāng)前目錄的相對(duì)目錄加載擴(kuò)展庫。

  --ini

展示配置文件名和掃描目錄。

示例 #6 --ini 示例

$ 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

展示指定函數(shù)或者類方法的有關(guān)信息(例如參數(shù)名稱和數(shù)量)。

如果 PHP 在編譯時(shí)啟用 Reflection 支持,該選項(xiàng)才可以使用。

示例 #7 基礎(chǔ) --rf 用法

$ php --rf var_dump
Function [ <internal> public function var_dump ] {

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

--rc --rclass

展示指定類的有關(guān)信息(常量、屬性和方法的列表)。

如果 PHP 在編譯時(shí)啟用 Reflection 支持,該選項(xiàng)才可以使用。

示例 #8 --rc 示例

$ 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

展示指定擴(kuò)展的有關(guān)信息(php.ini 選項(xiàng)、定義函數(shù)、常量和類的列表)。

如果 PHP 在編譯時(shí)啟用 Reflection 支持,該選項(xiàng)才可以使用。

示例 #9 --re 示例

$ 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 擴(kuò)展的配置信息(也可以通過 phpinfo()返回相同信息)。

--ri --rextinfo

展示指定擴(kuò)展的配置信息(也可以通過 phpinfo()返回相同信息)。 使用 “main” 作為擴(kuò)展名可以獲取到核心配置信息。

示例 #10 --ri 示例

$ 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

注意:

選項(xiàng) -rBRFEH、 --ini--r[fcezi] 僅可以在 CLI 中使用。