PHP in the Shell
PHP may be most commonly used within a web server to produce web pages, but it is a powerful scripting engine by itself. PHP is an amazingly useful multipurpose tool when used from the command line. This post will show you how to use the PHP Command Line Interface (CLI). Some of the information here is Linux specific, but there are equivalents for Windows.
Accessing the PHP CLI
Obviously the first thing you need to know is how to run PHP from the command line. There are a number of ways to do this, and one way that I prefer.
The method I find easiest is to write your script as a shell script. This basically means taking a regular PHP script and adding a line to the beginning declaring the php binary to interpret the script. Here's an example:
#!/usr/bin/php <?php echo 'Hello World'; ?>
Assuming the file name 'test.php' you would make it executable with chmod +x test.php and run it simply with ./test.php. Note that the file does not need to end with .php to function.
Other options for accessing the PHP CLI are to pass scripts or code to php as parameters, pass code to php through standard input, or enter code manually in interactive mode.
File as parameter:
$ php test.php
Code as parameter:
$ php -r "echo 'Hello World';"
Code through standard input (generate_php_code outputs code):
$ generate_php_code | phpInteractive mode:
$ php -a Interactive shell php > echo 'Hello World'; Hello World php >
Using Arguments
Like any shell scripts, you can pass arguments to your PHP scripts. Accessing these arguments is very easy, since they are stored in an array called $argv. The number of arguments is stored in $argc.
To illustrate this, running the following script as ./args.php arg1 arg2 arg3:
#!/usr/bin/php <?php var_dump($argv); ?>
will output:
array(4) {
[0]=>
string(8) "args.php"
[1]=>
string(4) "arg1"
[2]=>
string(4) "arg2"
[3]=>
string(4) "arg3"
}Practical Example
In this example, we'll create a script that outputs the number of days since a particular date. It will accept 3 parameters, the month, day, and year as numbers.
#!/usr/bin/php <?php if($argc < 4 || !is_numeric($argv[1]) || !is_numeric($argv[2]) || !is_numeric($argv[3])) { echo "Usage: $argv[0] mm dd yyyy\n"; } else { $pastdate = mktime(0, 0, 0, $argv[1], $argv[2], $argv[3]); $diff = time() - $pastdate; $days = round($diff/60/60/24); echo "$days days since $argv[1]/$argv[2]/$argv[3]\n"; } ?>
$ ./days 11 17 1988 7699 days since 11/17/1988
Standard Input/Output
PHP also makes it fairly easy to work with standard input and output. It even provides the constants STDIN, STDOUT, and STDERR that point to the already opened streams to stdin, stdout, and stderr respectively. You use them exactly as you would a handle to an open file.
Here's an example to demonstrate the usage. This shows how to read STDIN line by line:
#!/usr/bin/php <?php // treat STDIN like a file handle while(!feof(STDIN)) { $line = fgets(STDIN); } ?>
Practical Example
This example script takes a file list (generated by ls) and outputs an HTML table:
#!/usr/bin/php <?php echo '<table>'; echo '<tr><th>Perm</th><th>Links</th><th>User</th><th>Group</th><th>Size</th><th>Date</th><th>Time</th><th>Name</th></tr>'; while(!feof(STDIN)) { $line = fgets(STDIN); $fields = preg_split('#\s+#', $line, 8); echo '<tr>'; foreach($fields as $field) { echo '<td>' . $field . '</td>'; } echo '</tr>'; } echo '</table>'; ?>
Assuming the name 'ls2html' use this to generate an HTML formatted table of the file list in list.htm:
$ ls -l | sed 1d | ./ls2html > list.htm
Conclusion
The PHP CLI is a very powerful tool that can be put to many uses. This can be very useful for someone like me who has strong PHP skills, but lacks strong Bash skills. As you can see, there's really not much difference between scripting for the web and scripting for the command line.

December 15th, 2009 - 16:47
Interesting. PHP is mostly used only for web apps, but it could also be useful in the Shell.
In the Shell, I still prefer Python, though. I just don’t like starting variable names with $