Using PHP In the Command Line

cli

Have you ever thought of an idea to do something with PHP, and wanted to test it out? You probably have to go through the trouble of writing your script, starting a server, and testing it in your browser.

Perhaps you love using PHP, and would like to do more than web development with it. You might like it as a more readable version of Perl.

You might want to try using CLI PHP. You get all the features of PHP, but without needing to use your browser!

Note: This information is mainly for Linux/Unix-like systems, but similar instructions will apply for Windows and other operating systems.

Writing Your First CLI PHP Script

Why not start with a simple Hello World program?

#!/usr/bin/php
<?php
echo 'hello, world\n';
?>

As you can see, this looks much like a regular PHP program, except for the first line.

This script starts with a bangline. It is used by Unix-like systems in many programming languages to identify the correct interpreter. It consists of a comment, an exclamation mark, and the path to the PHP executable.

The \n inside the string is just to ensure a line break after printing it.

How to Call a CLI PHP Script

There are several ways to call a PHP script from the command line. The most common way is to call the PHP executable directly, with the filename as an argument.

php hello.php

You can also call the script directly. Make sure that the file permissions are set to "execute".

./hello.php

Another way is to have code directly inside the command, as an argument.

php -r "echo 'hello, world';"

Using Arguments

Command line arguments can also be used in CLI PHP, through two variables called $argc and $argv.

$argc is the number of arguments. If somebody runs the script with one argument, $argc will be equal to 2, because the name of the script counts as the first one.

$argv is an array of all of the command line arguments used. So $argv[0] will be the name of script, $argv[1] will be the first argument, and so on.

This is a very simple example demonstrating the use of command line arguments in PHP. It simply prints the first argument.

#!/usr/bin/php
<?php
if ($argc == 2) {
    echo "$argv[1]\n";
} else {
    echo "Usage: $argv[0] string\n";
}
?>

To run this script with a command line argument, we can call it like this:

./args.php foo

This will output foo.

The Interactive Shell

Another great feature of CLI PHP is the interactive shell, similar to that of Python. It allows you to type in statements as you're going, and see the results right away. The command to start the PHP interactive shell is:

php -a

In the interactive shell you should see a prompt like this:

Interactive shell
 
php >

After that prompt, you can type in commands in a similar way to what you'd do inside of a file. The only difference is that each statement is processed right when you hit enter. Here's an example:

Interactive shell
 
php > echo 'hello, world';
hello, world
php > $theWorldIsFlat = 1;
php > if ($theWorldIsFlat) {
php { echo 'Be careful not to fall off!';
php { }
Be careful not to fall off!
php >

As you can see, it handles statements typed, with enter pressed right after, and it also handles statement blocks, such as after the if statement.

The opening curly bracket ({) at the beginning of a statement is a part of the prompt, to let you know that you're currently inside of a block.

Conclusion

For now, I'm still going to stick to Python for writing quick command line scripts. But CLI PHP can be very useful, for creating and testing quick scripts.

Do you ever use CLI PHP? If so, what do you use it for?

Stay Updated

Did you enjoy this post? Don't miss a single post by getting free updates!

3 Comments

  1. January 29, 2010

    I use command line PHP all the time for both testing and running batch processes.

    Note that if you run it like php /path/to/script.php then you don't need to have the shebang line (the #!/etc part)

    • January 29, 2010

      You're right. That's a very good tip! I usually just leave it in anyway, though.

Trackbacks/Pingbacks