HomePHP Page 2 - Implementing with PHP: Standalone Scripts
Introduction to the PHP Command-Line Interface (CLI) - PHP
If you've ever been interested in making significant use of PHP outside of a web environment, this article will show you how. The first of three parts, it is excerpted from chapter five of the book Advanced PHP Programming, written by George Schlossnagle (Sams; ISBN: 0672325616).
If you built PHP with --enable-cli, a binary called php is installed into the binaries directory of the installation path. By default this is /usr/local/bin. To prevent having to specify the full path of php every time you run it, this directory should be in your PATH environment variable. To execute a PHP script phpscript.php from the command line on a Unix system, you can type this:
> php phpscript.php
Alternatively, you can add the following line to the top of your script:
and then mark the script as executable with chmod, as follows:
> chmod u+rx phpscript.php
Now you can run phpscript.php as follows:
This #! syntax is known as a "she-bang," and using it is the standard way of making script executables on Unix systems.
On Windows systems, your registry will be modified to associate .php scripts with the php executable so that when you click on them, they will be parsed and run. However, because PHP has a wider deployment on Unix systems (mainly for security, cost, and performance reasons) than on Windows systems, this book uses Unix examples exclusively.
Except for the way they handle input, PHP command-line scripts behave very much like their Web-based brethren.