CHAPTER 16 – How CLI Differs From CGI

The CLI version of PHP is quite similar to the CGI version, upon which it was once based. The main difference lies in all the web server integration, which is really what CGI is about. With CLI, PHP is trimmed down to the very basics, and imports no GET or POST form variables, outputs no MIME headers in the output, and generally does none of the behind-the-scenes that other SAPI implementations do. The CLI version of PHP behaves like any other script parser, such as Perl or Python. The one remaining proof of PHP's web heritage is the fact that you still need to use the <?php ?> tags around code.

16.2.1.1 Default Parameters CLI has different default values for a few command-line options and php.ini settings, as shown in Table 16.1. CLI DefaultDescription Table 16.1 CLI Default Options Setting/Option CLI/Default Description -q option Enabled Suppresses HTTP headers in output. -C option Enabled PHP does not change its working directory to that of the main script. html_errors Disabled Error messages from PHP will be in plain text rather than HTML. implicit _flush Enabled register_argc_argv Enabled The $argc and $argv global variables are registered regardless of the register_argc _arg settings in php.ini. max_execution_time 0 The longest time (in seconds) PHP lets scripts execute; 0 means no limit. 16.2.1.2 Extra Options There are some command-line options in PHP CLI that CGI does not offer, as shown in Table 16.2. Table 16.2 Extra CLI Options Setting/Option CLI Default Description -r code None Run code as PHP code (no <?php necessary). -R code None Run code as for every line on stdin. _B code None Run code before process- ing lines with -R or -F. -E code None Run code after process- ing lines with -R or -F. _F file None Execute file for every input line.

These options can be used to quickly execute some PHP code from the command line; for example: $ php ­r 'var_dump(urlencode("æøå"));' When using ­r, -R, -B, and -E, make sure that your PHP code is complete with the final semicolon.

Post Comment
Login to post comments