PHP 8.3.4 Released!

コマンドラインオプション

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 を対話的に実行します。詳細な情報は、対話シェル を参照ください。

-b --bindpath

外部 FASTCGI サーバーモードでのバインドパス (CGI のみ)。

-C --no-chdir

スクリプトのディレクトリへ 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

このオプションを指定しない場合、ファイルは、 デフォルトの位置 で探索されます。

-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 このオプションを使用すると、実際の一連のコマンドラインオプションと 各1行の説明が情報を取得できます。
-i --info このコマンドラインオプションは、phpinfo() をコールし、 結果を出力します。PHP が正しく動作していない場合、 php -i を実行し、情報テーブルの前または中に 出力されるエラーメッセージを調べることをお勧めします。 CGI モードの場合、 出力が HTML 形式で行なわれるため 量がかなり多くなることに注意してください。
-l --syntax-check

このオプションにより、指定した PHP コードの 構文チェックのみを簡単に行なうことができます。成功した場合、 テキスト No syntax errors detected in <filename> が標準出力に書き込まれ、リターンコードは 0 となります。失敗した場合、テキスト Errors parsing <filename> に加え、内部パーサエラーメッセージ が標準出力に書き込まれ、シェルリターンコードは、 -1 となります。

このオプションは、(未定義の関数のような)致命的なエラー(fatal error) はみつけません。致命的なエラーについても調べたい場合は、 -f を使用してください。

注意:

このオプションは、オプション -r と共に使用することは できません。

-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 の開始および終了タグ (<?php および ?>) は不要で、これらを付けると パーサエラーとなります。

注意:

このように PHP を実行する際に、コマンドラインの 変数がシェルにより行なわれる置換と干渉しないように注意してください。

例2 ダブルクォートの使用時に構文エラーが出る例

$ 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

ここでの問題は、2 重引用符 " を用いた場合でも sh/bash が変数置換を行うことです。 $foo はおそらく定義されていないので、 空文字列に展開された後、実行用に PHP に そのコードが渡され、以下のように読み込むことになります。

$ php -r " = get_defined_constants();"

正しい方法は、1 重引用符'を使用することです。 1 重引用符で括られた文字列の中の変数は、sh/bash により展開されません。

例3 シングルクォートを使い、シェルの変数置換を防ぐ例

$ 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 以外のシェルを使用している場合には、別の問題を経験したことが あるかもしれません。バグを » https://github.com/php/php-src/issues に報告してください。 シェル変数をコードに取得しようとしたり、エスケープを行なうために バックスラッシュを使用したりした場合にも、容易に問題を発生する 可能性があります。注意が必要です。

注意:

-rCLI SAPI で有効で、 CGI SAPI では使用できません。

注意:

このオプションはあくまで基本的な機能を提供するものです。そのため、 いくつかの設定ディレクティブ(例: auto_prepend_file および auto_append_file)は このモードでは無視されます。

-B --process-begin

標準入力を処理する前に実行する PHP コードを指定します。

-R --process-code

それぞれの入力行に対して実行する PHP コードを指定します。

このモードには 2 つの特別な変数 $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

ビルトインウェブサーバー を開始させます。

-t --docroot ビルトインウェブサーバー のドキュメントルートを指定します。
-s --syntax-highlight と --syntax-highlighting

カラー構文ハイライト表示されたソースを表示します。

このオプションは、ファイルをパースし、HTML ハイライト表示版のファイルを生成し、標準出力に書き出す内部機 構を使用します。行うのは <code> [...] </code> のブロックを 生成することだけで、HTML ヘッダは 出力されないことに注意してください。

注意:

このオプションは、-r オプションと同時に 使用することはできません。

-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

コメントと空白文字を削除してソースを表示します。

注意:

このオプションは、-r オプションと同時に 使用することはできません。

-z --zend-extension

Zend 拡張モジュールをロードします。ファイル名のみが指定された場合、 PHP はこの拡張をカレントのシステムのデフォルトライブラリパスから ロードしようとします (Linux システムの場合は /etc/ld.so.conf で 指定されています)。 ファイル名を絶対パスで指定した場合、システムのライブラリサーチパスを 使用しません。ディレクトリ情報を有する相対ファイル名を 指定すると、PHP は カレントのディレクトリの相対パスから拡張モジュールをロードする ことのみを行ないます。

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

指定した関数あるいはクラスメソッドについての情報 (たとえばパラメータの数と名前など) を表示します。

このオプションは、PHP が リフレクション のサポートつきでコンパイルされている場合にのみ使用可能です。

例7 基本的な --rf の使用法

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

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

--rc --rclass

指定したクラスについての情報 (定数、プロパティおよびメソッドの一覧) を表示します。

このオプションは、PHP が リフレクション のサポートつきでコンパイルされている場合にのみ使用可能です。

例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

指定した拡張モジュールについての情報 (php.ini オプション、 定義されている関数、定数およびクラスの一覧) を表示します。

このオプションは、PHP が リフレクション のサポートつきでコンパイルされている場合にのみ使用可能です。

例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 拡張モジュールについての情報 (phpinfo() が返す情報と同じもの) を表示します。

--ri --rextinfo

指定した拡張モジュールについての設定情報 (phpinfo() が返す情報と同じもの) を表示します。 コア機能に関する設定情報は、 "main" というモジュール名で取得できます。

例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

注意:

オプション -rBRFEH--ini および --r[fcezi]CLI でのみ使用可能です。

add a note

User Contributed Notes 3 notes

up
1
dch
4 months ago
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.ini
up
-1
Ap.Muthu
9 years ago
If 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.
up
-21
yanshinian at yeah dot net
5 years ago
get ./configure

php -i |grep configure

Configure Command => './configure' '--prefix=/app/webserver/php5' '--with-config-file-path=/app/webserver/php5/etc' '--with-config-file-scan-dir=/app/webserver/php5/etc/php.d' '--enable-inline-optimization' '--disable-debug' '--disable-rpath' '--enable-shared' '--enable-opcache' '--enable-fpm' '--with-fpm-user=appuser' '--with-fpm-group=appuser' '--with-mysql=mysqlnd' '--with-mysqli=mysqlnd' '--with-pdo-mysql=mysqlnd' '--with-gettext' '--enable-mbstring' '--enable-exif' '--enable-ftp' '--enable-wddx' '--with-iconv' '--with-mcrypt' '--with-mhash' '--with-openssl' '--enable-bcmath' '--enable-soap' '--with-libxml-dir' '--enable-pcntl' '--enable-shmop' '--enable-sysvmsg' '--enable-sysvsem' '--enable-sysvshm' '--enable-sockets' '--enable-gd-native-ttf' '--enable-gd-jis-conv' '--enable-calendar' '--with-curl=/app/webserver/curl' '--with-zlib' '--enable-zip' '--with-bz2' '--with-readline' '--with-gd' '--with-freetype-dir' '--with-png-dir' '--with-jpeg-dir' '--with-vpx-dir' '--with-xpm-dir' '--with-t1lib' '--disable-json'
To Top