PHP CLI
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 webserver (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 avaiable on all popular operating systems: Linux, Windows, OSX, Solaris. Popular Linux distibutions (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 installtion hassle free and you can start using it withing a seconds!
PHP CLI 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 enabeled 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 behaviors.
If you have strandart 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 enought not to have it buy defualt, 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 simpliest PHP CLI script on Linux would look like this:
#!/usr/bin/php -q
<?php echo "Hello world of PHP CLI!"; ?>
Windows user would need to amend the first line with appropriate windows style path to php.exe:
#!C:\php\php.exe -q
<?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) taks 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 multithreadingi capabilities
- access system STDIN, STDOUT, STERR with PHP
Command Line Interface and CRON : Runing 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 maintane customers accounts (email customers that their account has to be renewed in next X days, backup for files and folders, creating health cheakers (pinging and retriving your remote servers or web pages regulary 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 avaiable 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 avaiable both on Linux and Windows.
Command Line Interface and Your Components : Bet you have already written good nd re-usable classes or functions that you are using in every project. For example, database abastration 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 mltithreading. For example, you may spend 10 secod just to establish http connection when you fopening remote http page adn just 1 second to retrive 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!). Obviuosly, you will need powerfull enough CPU, enough memory and network bandwidth.