Scripting with PHP
Normally if I’m going to write a script to automate a task via cron, I’ll use Perl (or lately, Ruby). Recently though I needed to use PHP in order to take advantage of common code from a web application. I was expecting this to be an unpleasant experience, using PHP outside of web programming, its raison d’ĂȘtre. However I ended up being pleasantly surprised with the results.
There are a few cases where it may make sense to write scripts in PHP:
- Code reuse. This was my reason for going with PHP. If a script is related to a web application written in PHP, it’s good practice to avoid reinventing as much functionality as possible. This also makes it easier to write tests directly in PHP.
- Availability. PHP tends to be widely installed on most machines, especially those with a web server. If you’re not sure what type of system the script may be running on, PHP might be safest bet.
- Widely known. There are a lot of languages out there nowadays, but PHP tends to a common denominator among developers due to its age and ease of learning. This is useful if the code is being written in a team that doesn’t have the same knowledge base. Even if someone doesn’t know PHP, it’s usually pretty easy to pick up given it’s similarity to languages like C, Java and Perl.
Luckily, PHP makes it very easy to move out of the web area into scripting. Recent versions of PHP include a Command Line Interface (CLI) which allows code to be run outside of a web server. The CLI also provides for parsing of command-line arguments and easy access to stdin/stdout/stderr. I had never used the PHP CLI before but was able to pick it up very quickly.
My foray into PHP scripting was also made a lot easier though various helper functions. PHP includes quite a few functions that can come in handy when writing a script or doing administration. For file I/O, the file_get_contents()/file_put_contents() pair of functions can be used to read or write the contents of a file in a single line of code, and I found them to be particularly useful. PHP’s functions for dealing with the filesystem, image parsing, string manipulation, and database processing are also helpful and may come in handy for the system administrator.
So the next time you need to write a script, consider using PHP, especially if it relates to a web application that’s written in the language. And if the decision to use PHP is forced upon you, don’t despair, as it might be a lot easier than you think.
Additional Reading:
Using PHP from the command line
SysAdmin to SysAdmin: Scripting admin tasks with PHP
System Administration with PHP















May 23rd, 2007 at 11:43 am
I do a lot of command line scripting in PHP at my company, an US financial company for such tasks as stock price screening, news harvester, some kind of data mining: news classifcation, basic data analysing and generating financial reports..PHP execution speed in Linux is extremely impressive.
June 28th, 2007 at 3:28 pm
Never used command line in PHP.
I have been told some aspect of PHP can be made simple doing it this way, i will take a look into some ways of doing this.