付録 HコマンドラインからPHPを使う

PHPコマンドライン版は、デバッグ時やPHPの設定をテストしたいような場 合には便利であるばかりでなく、Webスクリプト作成以外の用途にPHPを使 用したい場合には、便利な選択肢です。

PHP実行ファイルの出力を外部ファイルに>文字で常にリダイレクトでき、 このため、php -q test.php > test.htmlは、 test.phpの出力をHTTPヘッダを付加せずに同じディ レクトリのtest.htmlファイルに出力します。

コマンドライン版は、PHP実行ファイルがある場合にのみ使用可能です。サー バモジュール版を構築し、CGI版が使用するマシンで利用可能でない場合、 使用することはできません。Windowsユーザの場合、PHP実行ファイルとサー バモジュールが共にバイナリパッケージに入っています。実行ファイルの 名前は、php.exeです。

以下にPHP 4.0.6に関するコマンドラインオプションを示します。 -h オプションを使用することにより、実際のリスト とその簡単な説明を得ることが可能です。php -h の 出力は、以下のようになります。

Usage: php [-q] [-h] [-s [-v] [-i] [-f <file>] |  {<file> [args...]}
  -q             Quiet-mode.  Suppress HTTP Header output.
  -s             Display colour syntax highlighted source.
  -f <file>      Parse <file>.  Implies `-q'
  -v             Version number
  -C             Do not chdir to the script's directory
  -c <path>      Look for php.ini file in this directory
  -d foo[=bar]   Define INI entry foo with value 'bar'
  -e             Generate extended information for debugger/profiler
  -z <file>      Load Zend extension <file>.
  -l             Syntax check only (lint)
  -m             Show compiled in modules
  -i             PHP information
  -h             This help

   

以下に最も重要なコマンドラインオプションのいくつかについて詳細な解 説を行います。

表 H-1コマンドラインオプション

オプション説明
-q HTTPヘッダ出力を抑制します。通常、PHPはコールするプログラム(す なわちWebサーバ)がブラウザに出力するようにHTTPヘッダを出力しま す。コマンドラインアプリケーションを作成する場合には、これらの ヘッダは意味がありません。
-s ファイル名で指定されたファイルをカラーハイライト表示にして出力 します。これは、PHPスクリプトでソースコードを highlight_file()により出力するのと同じです。
-v このオプションを付けてPHPをコールすると、バージョン番号、すな わち、4.0.6を知ることができます。
-C 通常、PHPは作業ディレクトリをスクリプトがあるディレクトリに変 更します。これにより、例えばファイル名を指定するだけで同じディ レクトリにあるファイルをオープンすることが可能となります。 このディレクトリ変更を無効にしたい場合はこのオプションを使用し て下さい。
-c このオプションを使用すると、別のphp.ini のパスを指定することが可能です。この場合、PHPは、デフォルトの パスの代わりに指定したパスで設定ファイルを探します。
-d このオプションを付けることにより、スクリプト実行時に、個々の php.ini設定を設定することが可能です。
-m このオプションにより、組み込まれた(そしてロードされた)PHPモジュー ルおよびZendモジュール、PHPおよびZendのバージョン番号、Zendの 著作権の簡略版を出力します。
-i このコマンドラインオプションは、phpinfo()を コールし、結果を出力します。PHPが正しく動作しない場合、 php -iを実行し、情報テーブルの前または中に 出力されているエラーメッセージを確認して下さい。
-h このオプションを付けると、実際のコマンドラインオプション及びそ の簡単な説明を得ることができます。

The PHP executable can be used to run PHP scripts absolutely independent from the web server. If you are on a Unix system, you should add a special first line to your PHP script, and make it executable, so the system will know, what program should run the script. On a Windows platform you can associate php.exe -q with the double click option of the .php files, or you can make a batch file to run the script through PHP. The first line added to the script to work on Unix won't hurt on Windows, so you can write cross platform programs this way. A simple example of writing a command line PHP program can be found below.

例 H-1コマンドライン実行用に作成されたスクリプト(script.php)


#!/usr/bin/php -q
<?php

if ($argc != 2 || in_array($argv[1], array('--help', '-help', '-h', '-?'))) {
?>

これは、オプションが1つあるコマンドライン版のPHPスクリプトです。

  使用法:
  <?php echo $argv[0]; ?> <option>

  <option> に出力したい何らかの単語を指定できます。
  --help, -help, -h, -? オプションを指定するとこのヘルプが表示されます。

<?php
} else {
    echo $argv[1];
}
?>

   

In the script above, we used the special first line to indicate, that this file should be run by PHP and should not print out HTTP headers. There are two variables you can use while writing command line applications with PHP: $argc and $argv. The first is the number of arguments plus one (the name of the script running). The second is an array containing the arguments, starting with the script name as number zero ($argv[0]).

In the program above we checked if there are less or more than one arguments. Also if the argument was --help, -help, -h or -?, we printed out the help message, printng the script name dynamically. If we received some other argument we echoed that out.

If you would like to run the above script on Unix, you need to make it executable, and simply call it as script.php echothis or script.php -h. On Windows, you can make a batch file for this task:

例 H-2 コマンドライン版PHPスクリプトを実行するバッチファイル(script.bat)


@c:\php\php.exe -q script.php %1 %2 %3 %4
  

Assuming, you named the above program as script.php, and you have your php.exe in c:\php\php.exe this batch file will run it for you with your added options: script.bat echothis or script.bat -h.