PHP Command-Line PHP Introduction - Web Development and Design | Tutorial for Java, PHP, HTML, Javascript PHP Command-Line PHP Introduction - Web Development and Design | Tutorial for Java, PHP, HTML, Javascript


Post Top Ad

Post Top Ad

Tuesday, July 16, 2019

PHP Command-Line PHP Introduction

PHP Command-Line PHP


PHP was created for web programming and is still used mostly for that purpose. However, PHP is also 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 website, 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 it also helps avoid maintenance overhead down the road.

PHP builds include a command-line interface (CLI) version. The CLI binary is similar to web server modules and 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'].

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:


You can then run the script just by typing its name on the command line, as long as the file has execute permission.

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 check if the return value of php_sapi_name() is cli. You can then branch your scripts’ behavior as follows:

       if ('cli' == php_sapi_name()) {
              print "Database error: ".mysql_error()."\n";
       } else {
              print "Database error.<br/>";

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.

One helpful option on the command line is the -d flag, which lets you specify custom INI entries without modifying your php.ini file. For example, here’s how to turn on output buffering:

       % php -d output_buffering=1 scan-discussions.php

The CLI binary also takes an -r argument. When followed by some PHP code without <?php and ?> script tags, the CLI binary runs the code. For example, here’s how to print the current time:

       % php -r 'print strftime("%c");'

For a list of complete CLI binary options, pass the -h command:

       % php -h

Finally, the CLI binary defines handles to the standard I/O streams as the constants STDIN, STDOUT, and STDERR. You can use these instead of creating your own filehandles with fopen():

       // read from standard in
       $input = fgets(STDIN,1024);

       // write to standard out

       // write to standard error

No comments:

Post a Comment

Post Top Ad