PHP CLI Command Line Scriting

It is sad that so many people think of PHP solely in terms of Apache and MySQL.  It is true that PHP is good at this, but it is an epic mistake to think that is the only use of PHP. It is also a mistake of gigantic proportions to think of command line scripts as nasty unfriendly programs that only execute on a black screen on the command line. When PHP is called command line, it is just to differentiate it from executing on Apache. It does not mean that PHP CLI is some kind of third rate prehistoric scripting nightmare.

While I am not advocating that you attempt to write a Monster App in PHP in command line mode, I would advocate that you pause and consider using it for everyday small tasks.

First off let us consider speed, starting with execution speed. PHP is not slow, nor does it consume vast amounts of resources unless you have written some exceptionally bad code. But let us get to the important aspect of speed, and that is the time taken to write the code from line one to delivery. In this department PHP is a world class leader, along with say Python.

Adding user friendly Window Dialogs without mastering an API the size of a telephone directory is also trivial in PHP. Simply use Zenity, it provides a wide selection of all the important dialogs, and if you have KDE you can use Kdialogs. This means you can have File Selection dialogs, Menus, ask for Passwords, and all the other important day to day tasks. With this stratergy, creating a Desktop Utility with proper Window Dialogs is a simple task, and not a herculean effort.

For example to popup a Window to grab a User name:

#!/usr/bin/php
<?php
exec( "kdialog --inputbox \"User Name\" ", $output, $ret_code );
echo "User Name is :".$output[0]."\n";
?>

This is very, very, very, easy to do. The Kdialogs entire library can be documented in one page, so its not going to take weeks to add the Windows…

You don’t need to go anal and strictly separate onto MVC Model Viewer Client, as doing the dialogs is not as complex as some print statements. If your using any kind of windows API that can not be documented in one page, and learnt in an hour… I feel truly sorry for you (when I’ve stopped laughing). Put it this way, the real reason behind the name Swing, is derived from the hangman’s noose..

Although that example was KDE specific, the Zenity Library is just as easy and can be run multi-platform, not just Linux, and not just KDE. So if you have the miss fortune to be stuck on Microsoft Console, you can still use Zenity.

Other things you can do that are trivial in PHP is launch your program as independent daemon process that runs in the background as or like a service. This is incredibly useful in the real world for problem solving. A micro light daemon can sit in the background and perform useful real world tasks.

This fragment shows how easy it is to fork off as a process :

$pid = pcntl_fork();
if( $pid ) {
  // parent process runs what is here
  exec("kill ".getmypid() );
  exit(0);
}
else {
  // independent child process runs what is here
  echo "child $pid\n";
}

Streams

That is not all you can do, there is the streams library, so its quiet plausible that you can communicate with your process by TCP/IP across the LAN or Internet. If you want, you can write a micro sized Server and Client in PHP in an afternoon. I say in an afternoon, because I am not suggesting socket programming, but using the stream library. The difference is that it would take days using sockets, assuming you know how to write sockets, if you have to learn about sockets we could be talking months.

Yup the streams library is one of PHP little secrets, and when you consider that most people only think of PHP in terms of executing from inside Apache it is not surprising.

This is because streams unifies the methods for working on files, sockets, and other similar resources. So you can read and write across the network pretty much like you can access a text file off the hardrive, or grab a web page, or do some FTP or what ever….

Sure there are better specialists libraries for any of these tasks, but using PHP Streams delivers working code in an afternoon, not a week or month latter. Most important of all is that it is a unified method for working with multiple resources. The same methods can be used over and over again be it to suck up a zip file or download some xml. You learn one way of doing it and you can read and write multiple resources.

For example, this page describes a simple yet functional chat server and client http://codeyoung.blogspot.co.uk/2009/07/simple-php-socket-based-terminal-chat.html

Conclusion

Anyway, I hope I have opened your mind to the rich world of possibilities that PHP has to offer.

The chief advantage is that you can get code up and running in a very short space of time to do useful jobs. I am not advocating that you try writing absolutely everything in PHP, or that PHP is the greatest language on the planet.

I am trying to point out that PHP  is so much more than just something to connect to MySQL inside Apache to spit out pretty webpages.

Leave a comment