PHP Command Line Use

 
Featuring: PHP on the Command Lines


 PHP has steadily progressed to the stage that command line usage of the PHP.exe is easier than ever before; especially with the new PHP 5 betas. This is most fortunate because it makes testing out little scriptlets of PHP code easier than ever. And with the new OOP and XML capabilities of PHP there are plenty of opportunities to test PHP fast and easily with command line test scripts and even simple batch harnesses that rip through 10 variations on a OOP definition theme. The side benefit ? Users suddenly discover that with regular expressions, great database connectivity, always great file I/O oiperations, easy array manipulation - in short the whole legacy Perl-like and enhanced features, PHP makes a great OS scripting agent.

The figure below shows the command options available when running PHP in command line mode.

The simplest use of PHP command line mode is to run a phpfile directly:
  php phptest.php
However before you do so it is worthwhile making the following changes to setups in to your running environ:
  1)put the folder containing the php.exe on the path. For Windows users this is -
    Path = c:\phpfolder;%Path%
  2)make a copy of the php.ini file and store it in this directory/folder;
  3)make the following changes to the php.ini file:
   a- output_buffering=off //in interactive mode buffering stalls output until buffer fill
   b- implicit_flush=on // this guarantees output is immediate
   c- max_execution_time = 30000 //second of elapsed time the program can run

The last setting, max_executon_time, is helpful when using the -a interactive mode option. Otherwise when running specific files you may not want to set max_execution_time because if a program has an infinite loop error it will eat up hours of execution time unless manually cancelled. The screen shot below shows a debug session done in command line mode - no browser, no localhost web server, just direct command line calls on PHP 5. This is very convenient for doing parsing tests or testing code - particularly file I/O or database report results.

The example above is typical. Here we are testing two things in PHP syntax. First, we want to see what happens when we use the splice function to add an associative array to an existing, container array which is acting like a stack, the array variable $xa in this case. The second is to see what happens when you pop an associative array item from the $xa stack array.

The results are very informative. First, splicing an associative array onto the stack is a destructive operation - the key is lost after the splicing as can be seen when the array is dumped with the print_r($xa) command. The next step shows that when the item is popped from the array/stack only the value is shown, not the key. Finally, popping an item from the stack does what it should - it deletes the item from the array/stack.

It is this type of quick check and testing of PHP code without need for browser or web server that makes the new PHP5 CLI-Command Line Interpreter so helpful. We use it also to do all sorts of DOS and Windows file and database manipulations since it is powerful and convenient.In sum, ytry the new command line mode of PHP 5 - it makes PHP much more approachable and exposes its adhoc utility capabilities. The result for this developer - no more complicated and hard to maintain .sh or .bat scripts.
   

 
Top of Page  Home   Tips & Tutorials
© Imagination 2001-2004