Tuesday, June 17, 2008

PHP Command Line Interface

If I may slightly increase the nerdiness quotient of this post (relative to my standard posts), I'd like to tell you about a concept that I have been giving some thought to lately. Anyone who has worked with servers (and maybe even desktop power users) is familiar with the concept of the scheduled task - basically, a script that runs regularly on some set schedule (e.g. nightly @ 2am). The conventional wisdom of scheduled tasks dictates that Windows users write in batch files (or, I guess, PowerShell, these days), and Linux/UNIX users script in bash or Perl. This is all well and good, but did you know that PHP can also be executed from the command line - it's not just a web-based language.

This is not a new addition to PHP; the PHP Command Line Interface (CLI) has been around for years, but, from what I can tell, seems to be underutilized. One tremendous benefit of using the PHP CLI is that you can reuse the libraries that you write on web versions of applications. So, for example, if you are writing a script that will run on a scheduled basis, but also want it be callable on-demand from a web app, you do not need to resort to system calls or rewriting the app - you can use the same code!

Plus, many web developers are familiar with PHP. So, you could enlist the help of some of your best web programmers to help you develop system utilities.

Anyway, sorry to deviate from my theme of "technologies that make your life easier," but just thought this might be useful to someone. :)

--Chris

No comments: