Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

Share this Page URL

Chapter 20. Client-Side PHP - Pg. 509

CHAPTER 20 Client-Side PHP 20.1 Introduction PHP was created for web programming and is still used mostly for that purpose. How- ever, newer versions of PHP are increasingly more capable as a general-purpose script- ing 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 509