PHP Command Line

Started by sukishan, Jul 14, 2009, 10:54 PM

previous topic - next topic
Go Down


Introducing the CLI
PHP's Command Line Interface first showed up in PHP 4.2.0, as experimental functionality. With PHP 4.3.0 the CLI became an official SAPI (Server API). What that means in practical terms is that the CLI version of PHP now comes as a separate PHP binary (executable), which you can use to run scripts on the command line, as well as providing "access points" to the rest of the world, much like the $_GET and $_POST variables that give you access to incoming data over the Web.

Before I go any further I should mention that, as you're probably aware, PHP is not the only choice for writing command line scripts. Both Perl and Python, to name just two, are widely used for writing command line applications and, in many cases, make better a choice than PHP. They provide a mature set of tools for common problems and, typically, better performance.

So, why use PHP? An obvious -- and very good -- reason may simply be that you know PHP better than the alternatives. Less obvious is that, if you're developing a Web application in PHP, writing supporting scripts in another language can lead to extra headaches -- even if you're confident in both. There's both the human aspect of having to switch programming "mind sets", and the overhead of having to support two platforms and the potential missed opportunities for re-use; data access logic may need to be implemented twice, for example.

What's more, if you're building a PHP application that will be used by "unknown" third parties, requiring them to set up other platforms in addition to PHP is likely to cause frustration, particularly if they're using Windows. Although PHP may not be the best choice overall for writing command line utilities, it's not a bad choice, either, and, in building tools to support an application that's already written in PHP, it makes sense to stick to a single platform.

What types of problems are suited to being solved with a command line script? Obvious choices are anything that relates to the administration of the server on which your Web application runs, such as backups, purging and archiving old data, and analysing log files -- perhaps sending an email alert should critical errors occur. These are all tasks that are suited to automation with cron, the "intended user" of the scripts being another script or application.

It needn't necessarily stop there, though. You might consider writing a command line "installer" for your Web application, to take care of copying PHP scripts to the right locations, setting up the correct file system permissions, loading the database schema and, for the intrepid, managing upgrades to the application. When executing PHP scripts over the Web via Apache, you typically don't have rights to complete tasks such as changing file system permissions - plus, you don't necessarily want to expose to the Web code that's capable of making radical changes. However, run from the command line, using a normal user account, the script can be kept offline while providing you the necessary rights to copy files, change permissions and so on.

The advantage of command line scripts is that they're usually easier to develop and, more importantly, quicker to use then Web-based of desktop GUIs. As such, they can also play a big part in your development process, helping you perform "build" tasks quickly and easily, such as generating API documentation with phpDocumentor or running your Simple Test suite. Phing (a build tool based on Apache's ANT) and rephlux (a continuous integration tool based on CruiseControl) provide frameworks to help you automate your development process.
A good beginning makes a good ending

Go Up

Quick Reply

With Quick-Reply you can write a post when viewing a topic without loading a new page. You can still use bulletin board code and smileys as you would in a normal post.

Warning: this topic has not been posted in for at least 120 days.
Unless you're sure you want to reply, please consider starting a new topic.

Note: this post will not display until it's been approved by a moderator.
Please leave this box empty:

Type the letters shown in the picture
Listen to the letters / Request another image

Type the letters shown in the picture:

shortcuts: alt+s submit/post or alt+p preview
IT Acumens Web Designing Chennai | GinGly :: Build your Personal Website | CineBuzz :: Cinema News | My Kids Diary :: Gift your Kids Memories :: Book Website @ 349 Rs monthly
Copyright 2005 - 2021 :: IT Acumens :: All Rights Reserved. :: XML Sitemap
ITAcumens Discussion Forum with 2 lakhs post running for 15 years - Powered by IT Acumens Pro Dedicated Server

My Kids Diary