PHP was created for web programming and is still used mostly for
that purpose. However, newer versions of PHP are increasingly more capable as a
general-purpose scripting language. Using PHP for scripts you run from the
command line is especially helpful when they share code with your web
applications. If you have a discussion board on your web site, you might want to
run a program every few minutes or hours to scan new postings and alert you to
any messages that contain certain keywords. Writing this scanning program in PHP
lets you share relevant discussion-board code with the main discussion-board
application. Not only does this save you time, but also helps avoid maintenance
overhead down the road.
With the PHP-GTK
extension, your command-line PHP programs can be full-featured GUI applications.
These can also share code with PHP web applications and text-based command-line
programs. Like PHP, PHP-GTK is cross-platform, so the same code runs on Unix and
Windows.
The same PHP binary built to be executed as a CGI program can
be run from the command line. To run a script, pass the script filename as an
argument:
% php scan-discussions.php
On Unix, you can also use the "hash-bang" syntax at the top of your scripts to run the
PHP interpreter automatically. If the PHP binary is in /usr/local/bin, make the first line of your script:
#!/usr/local/bin/php
You can then run the script just by typing its name on the
command line, as long as the file has execute permission.
Command-line PHP scripts almost always use the -q
flag, which prevents PHP from printing HTTP response headers at the beginning of
its output:
% php -q scan-discussions.php
You can also use this:
#!/usr/local/bin/php -q
Another helpful option on the command line is the -c
flag, which lets you specify an alternate php.ini
file to load settings from. If your default php.ini file is /usr/local/lib/php.ini, it can be helpful to have a
separate configuration file at /usr/local/lib/php-commandline.ini with settings such
as max_execution_time = 0; this ensures that your scripts don't quit
after 30 seconds. Here's how to use this alternate file:
% php -q -c /usr/local/lib/php-commandline.ini scan-discussions.php
You can also use this :
#!/usr/local/bin/php -q -c /usr/local/lib/php-commandline.ini
If it's likely that you'll use some of your classes and
functions both for the web and for the command line, abstract the code that
needs to react differently in those different circumstances, such as HTML versus
plain-text output or access to environment variables that a web server sets up.
A useful tactic is to make your code aware of a global variable called $COMMAND_LINE. Set this to
true at the top of your command-line scripts. You can then branch your
scripts' behavior as follows:
if ($GLOBALS['COMMAND_LINE']) {
print "Database error: ".mysql_error()."\n";
} else {
print "Database error.<br>";
error_log(mysql_error());
}
This code not only adjusts the output formatting based on the
context it's executing in (\n versus <br>), but also
where the information goes. On the command line, it's helpful to the person
running the program to see the error message from MySQL, but on the Web, you
don't want your users to see potentially sensitive data. Instead, the code
outputs a generic error message and stores the details in the server's error log
for private review.
Beginning with Version 4.3, PHP builds include a command-line interface (CLI)
binary.[1] The CLI
binary is similar to the CGI binary but has some
important differences that make it more shell-friendly. Some configuration
directives have hardcoded values with CLI; for example, the html_errors
directive is set to false, and implicit_flush is set to
true. The max_execution_time directive is set to 0, allowing
unlimited program runtime. Finally, register_argc_argv is set to
true. This means you can look for argument information in
$argv and $argc instead of in $_SERVER['argv'] and
$_SERVER['argc']. Argument processing is discussed in Section 20.2 and Section 20.3.
[1] The CLI binary can be built under 4.2.x versions by explicitly configuring PHP with --enable-cli.



