PHP from the Command Line

PHP is capable of being more than just a scripting language for web sites. While it is far and away most encountered when serving web site content, you can also use PHP from the Command Line as a general purpose scripting language.

There are several more traditional languages used for general purpose scripting duties such as Bash, TCK, Perl and Python. While it could be argued that some of these languages are nicer languages than PHP or better suited to certain tasks, there is no denying that PHP is extremely popular.

Considering the sheer number of PHP programmers in the world today, some may find it efficient to leverage their existing knowledge of PHP rather than to learn a new programming language.

PHP can also be used to build Applications that deliver full graphical user interfaces (GUIs). For example, PHP-GTK provides an object-oriented interface to GTK+ classes and functions. However, this article will be concentrating on using PHP from the Command Line.

Installing the PHP CLI

You do not require a Web Server to use PHP from the command line. The PHP CLI (Command Line Interpreter) will run equally well from either a Workstation or a Server. Installing the CLI version of PHP in a Debian or Ubuntu Linux distribution is very simple.

sudo apt-get install php5-cli
This article demonstrates using the PHP Command Line Interpreter from a Debian style Linux system. If you are using Microsoft Windows, see the official PHP guide to installing PHP Manually.

Testing PHP from the Command Line

You can quickly test PHP from a terminal by asking it to display which version of PHP is currently installed.

php -v

PHP supports a variety of command line arguments. Invoking PHP’s built-in help will display these.

php -h

Creating and executing a PHP Script

Using your favorite text editor, create a new file entitled hello.php. Enter the following PHP code and save the file somewhere convenient for testing.

<?php

echo "Hello Human!\n";

?>

From a terminal, navigate to the directory where you stored hello.php. Launch the script as below and you will receive output directly from PHP.

php hello.php
Hello Human!

As you can see, you only need to provide PHP with the filename of the script that you want to launch. The output from PHP is passed to stdout and appears directly in the terminal rather than being rendered as a webpage.

The Shebang Line

While not required, it is possible to make a PHP script executable simply by invoking it directly from a terminal.

./hello.php

Before this will work however, two things must be addressed. First the hello.php file must be given permission to allow it to be executed. The chmod command is a simple way of achieving this from your Linux terminal.

chmod u+x hello.php

Next we need to include a line of code at the top of the PHP file known as the Shebang line. This will inform the system of precisely where your PHP Interpreter is located. If you do not know where PHP is installed you should be able to locate it by using the which command.

which php
/usr/bin/php

Once you know exactly where PHP is installed on your system, simply add the single Shebang Line to the top of your PHP script.

#!/usr/bin/php
<?php

echo "Hello Human!\n";

?>

Finally you can execute the script directly from the command line.

./hello.php
Hello Human!
File Permissions are unaffected by typical file editing. You will not need to update the permissions of your file every time you edit the source code.

Conclusion

This article prepared you for using PHP from the Command Line as a general purpose scripting language. Look for more articles in the future that will provide further explanations on this topic along with examples of using PHP in this way for a variety of tasks.

Speak Your Mind

*