Appendix D. Using PHP from the Command Line

Although PHP scripts are most commonly run through a Web server — either via an Apache or IIS module, or using the CGI version of PHP — it's possible to run the PHP engine in command-line mode. This lets you create stand-alone PHP scripts that can be run directly from the command line, or by double-clicking an icon. This means you can build scripts that:

  • Can be run on any computer with PHP installed, without needing a Web server

  • Can be scheduled to run automatically at certain times of the day or week

  • Can have a GUI (graphical user interface), much like a regular Windows, Mac, or Linux GUI application

  • Can be called by other PHP scripts or applications to carry out specific tasks

If you're used to other command-line scripting languages such as Perl, Tcl, or Bash, PHP in command-line mode works in a similar fashion.

On most UNIX-like systems, including Ubuntu and Mac OS X, you can run the command-line version of PHP simply by typing php at a shell prompt. For example, type php -v to display version information:

$ php -v
PHP 5.3.0.0-dev (cli) (built: Jun 29 2009 21:25:23)
Copyright (c) 1997-2009 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2009 Zend Technologies

To run the MAMP-specific version of PHP on Mac OS X with MAMP installed, instead of the built-in Mac OS X version of PHP, you'll need to specify the full path to the PHP executable (for example, /Applications/MAMP/bin/php5/bin/php -v) or add the /Applications/MAMP/bin/php5/bin folder ...

Get Beginning PHP 5.3 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.