The PHP language’s popularity has brought about many experimental projects which stretch PHP’s application well beyond the Web (several of which I documented in the July 2010 Developer.com article 10 Experimental PHP Projects Pushing the Envelope). For instance, did you know that it’s possible to build client-side GUI applications using PHP-GTK? Or that you can manage mailing lists with hundreds of thousands of subscribers using phplist?
Of course, just because you can do something with a language doesn’t necessarily mean you should. However, I regularly use an alternative application of PHP and I’m quite surprised that this capability remains relatively unknown. I’m referring to the ability to run PHP scripts from the command line using its command line interpreter (CLI). Even though it’s been possible since the PHP 4.3.0 release, you may be completely unaware of this CLI usage unless you employ great tools such as PHPDoc, Phing, or PHPUnit. Running PHP scripts with CLI allows you to leverage your PHP language skills whenever you need to run scripts from the shell.
Why Use the PHP CLI?
PHP is intended primarily to run in conjunction with a Web server, responding to requests that involve PHP-enabled Web pages. In order to prevent a poorly written PHP script from running indefinitely or consuming too much system memory, PHP is configured with both execution duration and memory consumption limitations of 30 seconds and 128MB, respectively (these default values can be changed using the
memory_limitconfiguration directives). Any PHP script that surpasses either limit will cause the script to halt execution immediately and produce a fatal error.
Therefore, you should use PHP’s CLI when you need to complete tasks that could conceivably surpass these limits, because the CLI takes neither limitation into account. For instance, I regularly use the CLI to run PHP scripts that use the Zend Framework’s Zend_Service_Amazon component to perform batch pricing updates for a video game database I internally maintain. This update often runs for well over 30 seconds due both to the sheer number of games in the database and occasionally slow internet connections, making this update an ideal candidate for the CLI.
Using the PHP CLI
Unless your PHP distribution was built using the
--disable-clioption, the CLI is automatically available on all PHP installations version 4.3.0 and newer. As of PHP 5, Windows users will find the CLI binary in the PHP installation directory, while most Unix/Linux users will find the CLI in their system path (with the location dependent upon PHP’s installation location). In either case, you should be able to retrieve some version-related information about your installed PHP CLI by executing the following command:
$ php -v PHP 5.3.2 (cli) (built: Apr 14 2010 13:04:45) Copyright (c) 1997-2010 The PHP Group Zend Engine v2.3.0, Copyright (c) 1998-2010 Zend Technologies with Xdebug v2.1.0, Copyright (c) 2002-2010, by Derick Rethans
Creating PHP CLI Scripts
Any PHP script executed via the CLI looks identical to its Web-based sibling. For instance, the following script is a perfectly valid CLI script:
<!--p printf("Today is %s", date('F j, Y')); -->
Save this file under the filename
php_date.phpand execute it from the command-line like this:
$ php php_date.php Today is December 23, 2010
You can eliminate the need to reference the CLI binary by added the “shebang” declaration to the beginning of your scripts. This defines the location of your CLI binary, which on Unix/Linux you can determine using the
$ which php /usr/bin/php
When determined, add the “shebang” to the script:
#!/usr/bin/php <!--p printf("Today is %s", date('F j, Y')); -->
Finally, make the script executable using the
chmodcommand (this step is not necessary on Windows):
$ chmod +x php_date.php
Now you should be able to execute the PHP script like this:
$ php_date.php Today is December 23, 2010
If you prefer, you can even remove the
.phpextension, allowing you to execute the script like this after having renamed it:
$ php_date Today is December 23, 2010