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.
コマンドライン実行用に作成されたスクリプト(script.php)
これは、オプションが1つあるコマンドライン版のPHPスクリプトです。
使用法:
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:
コマンドライン版PHPスクリプトを実行するバッチファイル(script.bat)
<email protected>:\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.