PHP from the Command Line

The command-line interface (CLI) was first introduced in PHP 4.3 (Dec. 2002) as its default Server API (SAPI). Earlier it was CGI.

The PHP command-line option -v (version) will reveal whether your Server API is a CLI binary or some other in your current installation

				
					php -v
				
			

In this tutorial, we look at the various ways to run PHP scripts from the command-line. We start with our simple 'Hello, World!' PHP program

				
					<?php 
						echo 'Hello, World!';  
					?>
				
			

Save it as hello.php. This file can be executed from the terminal by running the command

				
					php hello.php
				
			

We can also use the -f option (file) as follows

				
					php -f hello.php
				
			

Shell Script

To execute the above 'Hello, World!' PHP code as a shell script, add the following shebang interpreter directive to the file as its first line of script (the path could be different for different operating systems; type which php to confirm)

					
						#!/usr/bin/php
						<?php 
							echo 'Hello, World!';  
						?>
					
				

Save it. Next, we type a command in the terminal to mark hello.php as executable

					
						chmod u+x hello.php
					
				

The script file can now be executed from the terminal by running the command

					
						./hello.php
					
				

PHP Interactive Mode/Shell

We first get the list of available PHP command line options along with their brief single-line descriptions by executing the command

					
					php -h

The output on the terminal looks something like this

PHP Command-Line Options

We note that the option -a starts either the PHP interactive mode/shell. We run the command

					
					php -a

and test a simple PHP statement echo "Hello, World!";

php -a

Though it shows that it has started the interactive mode (not the interactive shell), for higher versions of PHP 5 (mine is v. 5.5.9), it actually is an interactive shell (or at least behaves like one). For if it is an interactive mode, enclosing PHP codes within the delimiters <?php and ?> would have been a prerequisite, followed by pressing Ctrl + D thereafter for parsing. It can also be verified by typing php -m, and you will find the readline module already listed. In an interactive shell, starting a PHP script with <php? actually throws you an error

Facebook's PHP Interactive Shell

Back in 2006, Facebook also released an interactive shell for PHP called phpsh. It can be installed following the commands below

					
					sudo git clone https://github.com/facebook/phpsh
					cd phpsh
					sudo python setup.py install

The interactive shell can be launched by typing the command

					
					phpsh

It will appear as below

Facebook's PHP Interactive Shell

Notes

  • The phpinfo() function gives a heap of detailed information about your current installed PHP like version, file locations, configuration, predefined variables, environment, HTTP headers, etc. We can obtain the same using the option -i
    							
    								php -i