Home arrow PHP arrow 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).

  1. Implementing with PHP: Standalone Scripts
  2. Introduction to the PHP Command-Line Interface (CLI)
  3. Handling Input/Output (I/O)
  4. Parsing Command-Line Arguments
By: Sams Publishing
Rating: starstarstarstarstar / 8
August 31, 2006

print this article



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:

#!/usr/bin/env php

and then mark the script as executable with chmod, as follows:

> chmod u+rx phpscript.php

Now you can run phpscript.php as follows:

> ./phpscript.php

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.

>>> More PHP Articles          >>> More By Sams Publishing

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates


Dev Shed Tutorial Topics: