What is PHP CLI? PHP CLI is a short for PHP Command Line Interface. As the name implies, this is a way of using PHP in the system command line. Or by other words it is a way of running PHP Scripts that aren't on a web server (such as Apache web server or Microsoft IIS). People usually treat PHP as web development, server side tool. However, PHP CLI applies all advantages of PHP to shell scripting allowing to create either service side supporting scripts or system application even with GUI!
PHP CLI is available on all popular operating systems: Linux, Windows, OSX, Solaris. Popular Linux distributions (such as Ubuntu, Debian, Fedora Core, Suse and etc.) allow to install PHP CLI from package manager (e.g. Synaptic or similar) with couple of mouse clicks. This makes installation hassle free and you can start using it within a seconds!
SAPI was first released in PHP 4.2.0 as experimental, but as of version PHP 4.3.0 (including PHP5), it is fully supported and enabled by default
. PHP CLI is just a new SAPI type (Server Application Programming Interface) that focused on developing shell (or desktop as well) applications with PHP. It's worth mentioning that PHP CLI and PHP CGI
are different SAPI's although they do share many of the same behaviours.
If you have standard installation of PHP for Apache web server, then there are very high chances that you already have PHP CLI installed on your system. You chances are even higher if your system is running Linux. If you unlucky enough not to have it buy default, then you need to recompile your PHP with the --enable-cli flag or reinstall from the package that does have it. If you are running Windows, then you probably need to add php executable to your system path.
The simplest PHP CLI script on Linux would look like this:
<?php echo "Hello world of PHP CLI!"; ?>
Windows user would need to amend the first line with appropriate windows style path to php.exe:
<?php echo "Hello world of PHP CLI!"; ?>
Why to use PHP CLI?
One want to use PHP CLI SAPI simply because there are several advantages in being able to run PHP code from command line such as:
- no need to learn another language such as Perl, Bash or Awk
- running scheduled (CRON) tasks written in php
- making GUI applications with PHP and GTK
- reusage of your existing components
- write very robust scripts for your system by using PHP5 multithreading capabilities
- access system STDIN, STDOUT, STERR with PHP
Command Line Interface and CRON : Running PHP scripts in CRON allows you to do scheduled tasks. For example, check weather links on your links page are still alive or run scheduled billing or maintain customers accounts (email customers that their account has to be renewed in next X days, backup for files and folders, creating health checkers (pinging and retrieving your remote servers or web pages regularly to make sure they are working and emailing you if they are down or slow), staring other programs) or anything else that you can imagine. CRON is available only for Linux. However, if you are a Windows user you may use AT command to schedule execution time.
Command Line Interface and GTK : YES! You can do GUI (graphical user interface) applications for your Windows or Linux box with PHP! All you need is PHP Command Line Interface and GTK in a pack. This will allow to create really portable GUI applications and won't require to learn anything else. Just PHP. GTK is available both on Linux and Windows.
Command Line Interface and Your Components : Bet you have already written good and re-usable classes or functions that you are using in every project. For example, database abstraction layer or just a DB connection function or it might be a logging script or date calculation class / library. With PHP Command Line Interface you can reuse them in command line without reinventing the wheel by doing the same in Perl or any other command line enabled language.
Command Line Interface and PHP5 Multithreading : If you have a bottle neck in database or network connection then you can speed up your script up to 1000% just by implementing PHP5 multithreading. For example, you may spend 10 seconds just to establish http connection when you fopening remote http page and just 1 second to retrieve the content. If you need to fopen 1000 pages one by one then you will spend 10*1000+1*1000 = 11000 seconds (3 hours and 3 minutes)! If you run 100 threads then you will spend (10*1000+1*1000)/100 = 110 seconds (less then 2 minutes!). Obviously, you will need powerful enough CPU, enough memory and network bandwidth.