Introduction
In this short article I will introduce you to the PHP Command Line Interface (or CLI for short). PHP CLI allows you to run PHP scripts directly from the command line bypassing the need for a web server.
Note: This article assumes that you are using PHP v4.3.0 or higher.
Why is it useful?
The PHP CLI can be useful for writing scripts that you want to run in the background or via cronjobs such as backups, pruning log files or running programs. PHP CLI scripts are also useful for testing small chunks of code without having to upload it to your web server first.
Handling Input and Output
In most scripts you will want to get some input from the user such as asking them to select an option and send some output back to them such as displaying the results
Outputting information to the screen is essentially the same as when using a web server. Using echo and print are the recommended ways to send output.
PHP Code:
<?php
echo "Look! I'm some output!\n";
print 'So am I!';
Code:
Look! I'm some output! So am I!
As with most scripts, you will eventually wish to get some input from the user. The PHP CLI provides a few ways to do this. The most common is to use the fgets() function. fgets() will read everything the user types until they press Enter.
PHP Code:
<?php
echo 'Please enter your first name: ';
$firstName = fgets(STDIN);
echo 'Hello ' . $firstName;
Note: STDIN and STDERR
STDIN and STDOUT are special streams provided by PHP for Input and Output. STDIN (STandarD INput) will contain all input from the user and STDOUT (STandarD OUTput) will contain all output that gets sent to the user.
So using fgets(STDIN) means that we want to fetch input from the Standard Input stream using the fgets() function.
There are many other functions for fetching user input such as fgetc and sscanf which are worth investigating further
Parsing parameters / arguments to your CLI script
You will frequently find yourself needing to parse command line arguments to your CLI scripts.
In a web-based application you would typically do this by adding them to the URL:
http://www.example.com/myscript.php?...something=else
This would result in the following variables becoming available in your PHP script:
PHP Code:
$_GET['this'] // Contains "that"
$_GET['something'] // Contains "else"
Code:
php myscript.php something else
To use these within your script you need to take advantage of the $_SERVER superglobal array. Specifically, $_SERVER['argc'] and $_SERVER['argv'].
$_SERVER['argc'] (argument count) contains the number of arguments passed to your script. It's important to note that this number includes the name of the script itself.
For example, if your script contained the following:
PHP Code:
<?php
echo $_SERVER['argc'];
Code:
php myscript.php Alan Wagstaff TalkPHP
If we modify our script to use $_SERVER['argv'] (argument values) to print a list of the arguments we pass we can see this in action:
PHP Code:
<?php
print_r($_SERVER['argv'];
Code:
php myscript.php Alan Wagstaff TalkPHP
Code:
Array
(
[0] => e:\webroot\talkphp\cli_basics\myscript.php
[1] => Alan
[2] => Wagstaff
[3] => TalkPHP
)
Conclusion
Hopefully this has given you an introduction to the basics of writing command line applications using PHP. For further reading on the PHP CLI, check out the links below.
Further Reading
PHP: Using PHP from the command line - Manual
PHP CLI


Join the friendly bunch on IRC...