2024 年日本 PHP 研討會

命令列選項

可以隨時透過使用 -h 參數執行 PHP 來查詢 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 的目錄,或自訂的 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 使用 -d 設定 INI 設定的範例

# Omitting the value part will set the given configuration directive to "1"
$ php -d max_execution_time
        -r '$foo = ini_get("max_execution_time"); var_dump($foo);'
string(1) "1"

# Passing an empty value part will set the configuration directive to ""
php -d max_execution_time=
        -r '$foo = ini_get("max_execution_time"); var_dump($foo);'
string(0) ""

# The configuration directive will be set to anything passed after the '=' character
$  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 和 -? --help 和 --usage 輸出命令列選項列表,並附帶一行說明其功能。
-i --info 呼叫 phpinfo() 函式,並印出結果。如果 PHP 無法正常運作,建議使用 php -i 命令,並查看在資訊表格之前或表格中是否有任何錯誤訊息。請注意,使用 CGI 模式時,輸出格式為 HTML,因此檔案非常大。
-l --syntax-check

檢查語法但不執行給定的 PHP 程式碼。如果未指定檔名,則會處理來自標準輸入的輸入,否則會檢查每個檔名。成功時,文字 No syntax errors detected in <filename>(在 <filename> 中未偵測到語法錯誤)會寫入到標準輸出。失敗時,除了內部剖析器錯誤之外,文字 Errors parsing <filename>(剖析 <filename> 錯誤)也會寫入到標準輸出。如果在指定的檔案(或標準輸入)中發現任何錯誤,則 shell 返回碼會設為 -1,否則 shell 返回碼會設為 0

此選項無法找到需要執行程式碼才能發現的嚴重錯誤(例如未定義的函式)。

注意:

在 PHP 8.3.0 之前,只能指定一個要檢查的檔名。

注意:

此選項不能與 -r 選項一起使用。

-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 的開始和結束標籤(<?php?>)**不需要**,如果存在將會導致剖析錯誤。

注意:

使用這種形式的 PHP 時必須小心,不要與 shell 執行的命令列變數替換衝突。

範例 #3 使用雙引號時發生語法錯誤

$ 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

這裡的問題是 sh/bash 即使使用雙引號 " 也會執行變數替換。由於變數 $foo 不太可能被定義,它會展開為空,導致傳遞給 PHP 執行的程式碼實際上讀取為

$ php -r " = get_defined_constants();"

正確的方法是使用單引號 '。單引號字串中的變數不會被 sh/bash 展開。

範例 #4 使用單引號防止 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"]=>
  [...]

如果使用 sh/bash 以外的 shell,可能會遇到其他問題 - 如果適當,應在 » https://github.com/php/php-src/issues 開立錯誤報告。在命令列程式碼中嘗試使用變數(shell 或 PHP),或使用反斜線進行跳脫時,仍然很容易遇到問題,因此在執行此操作時要格外小心。我們已經警告過你了!

注意:

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

注意:

這個選項僅適用於非常基本的程式碼,因此在此模式下會忽略某些組態指令(例如 auto_prepend_fileauto_append_file)。

-B --process-begin

在處理標準輸入前執行的 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

啟動 內建網路伺服器

-t --docroot 指定 內建網路伺服器 的文件根目錄。
-s --syntax-highlight 和 --syntax-highlighting

顯示彩色語法高亮顯示的原始碼。

此選項使用內部機制來解析檔案,並將其 HTML 高亮顯示版本寫入標準輸出。請注意,它所做的只是產生一個 <code> [...] </code> HTML 標籤區塊,沒有 HTML 標頭。

注意:

此選項不能與 -r 選項一起使用。

-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

顯示已去除註釋和空白的原始碼。

注意:

此選項不能與 -r 選項一起使用。

-z --zend-extension

載入 Zend 擴充功能。如果只提供檔名,PHP 會嘗試從系統目前的預設程式庫路徑載入此擴充功能(例如,通常在 Linux 系統上為 /etc/ld.so.conf)。傳遞帶有絕對路徑的檔名將不會使用系統的程式庫搜尋路徑。包含目錄資訊的相對檔名將指示 PHP 嘗試載入相對於目前目錄的擴充功能。

  --ini

顯示組態檔名稱和掃描的目錄。

範例 #7 --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

顯示關於指定函式或類別方法的資訊(例如,參數的數量和名稱)。

此選項僅在 PHP 使用 Reflection 支援編譯時才可用。

範例 #8 基本 --rf 用法

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

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

--rc --rclass

顯示關於指定類別的資訊(常數、屬性和方法的列表)。

此選項僅在 PHP 使用 Reflection 支援編譯時才可用。

範例 #9 --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

顯示關於指定擴充功能的資訊(php.ini 選項、已定義函式、常數和類別的列表)。

此選項僅在 PHP 使用 Reflection 支援編譯時才可用。

範例 #10 --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 擴充功能的組態資訊(與 phpinfo() 傳回的資訊相同)。

--ri --rextinfo

顯示指定擴充功能的組態資訊(與 phpinfo() 傳回的資訊相同)。核心組態資訊可以使用「main」作為擴充功能名稱來取得。

範例 #11 --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

注意:

選項 -rBRFEH--ini--r[fcezi] 僅在 命令列介面 (CLI) 中可用。

新增註記

使用者貢獻的註記 2 則註記

dch
1 年前
如果您想使用 shell 命令列中的 -i 參數搭配指定的 php.ini 查看 PHP 的目前設定,則參數的順序很重要。將 -i 放在 -c 之後會得到預期的結果。

印出預設 php.ini (cli) 的資訊
$ php -i --php-ini /etc/php/7.4/fpm/php.ini | grep -i "loaded conf"
載入的設定檔 => /etc/php/7.4/cli/php.ini

印出所需 php.ini (fpm) 的資訊
$ php --php-ini /etc/php/7.4/fpm/php.ini -i | grep -i "loaded conf"
載入的設定檔 => /etc/php/7.4/fpm/php.ini
Ap.Muthu
10 年前
如果我們使用以下指令啟動 PHP 的內建網路伺服器(PHP v5.4 以後版本):
php -S localhost:8000 -t htdocs
並且其中有一個圖片檔案 picture.jpg
並在 HTML 頁面中使用以下程式碼參考它:
<img src="picture.jpg">
則渲染後的頁面將不會顯示圖片,且圖片後面的 HTML 程式碼為:
https://127.0.0.1:8000/index.php/picture.jpg

然而,如果頁面中的 HTML 程式碼為:
<img src="/picture.jpg">
則圖片會正確顯示。

因此,在 PHP 5.4.33 Win32 VC9 版本中,相對路徑定址功能已損壞。
To Top