Core CLI DifferencesWhen you compare the CLI version of PHP to the standard version used for Web development, relatively few differences exist between the two. In fact, the only things that have been altered or removed from the CLI version of PHP do not apply to non-Web development (such as error messages with HTML formatting). Let's start with what has changed in terms of the code of PHP. In the command-line version of PHP, the first major difference is the array of command-line arguments that PHP accepts. Although you may have been familiar with some of these arguments working with a CGI executable of PHP, a few CLI-specific ones have been added. A complete list of which command-line arguments PHP accepts are shown in Table 17.1. Along with the new and/or different command-line options available to PHP developers, a handful of configuration directives have also been removed with the CLI version of PHP. For the most part, these directives have no practical use when you're working from a command line and hence have been removed. Specifically, the following configuration directives have been disabled in the CLI version of PHP: Under most circumstances, you do not need to use the parameters outlined in the preceding table. After you have written a script that you would like to run from the command line, provide the command line and path of the script to execute: $ php /usr/local/scripts/my_cli_script.php To simplify the execution of PHP scripts even further on Unix-based systems, you may specify the CLI version of PHP within the script itself in the following fashion: #!/usr/local/bin/php <?php echo "Hello, world!\n"; ?> Using this method, the script can be executed directly from the command line as shown next by setting the execute bit as shown (assume that the preceding script is called myscript): $ chmod u+x ./myscript $ ./myscript Hello, world! $ Another difference between other versions of PHP and the CLI version is the addition of three predefined constants: STDIN, STDOUT, and STDERR. These three constants in the CLI version of PHP are automatically created for every script and are file references to their appropriate input/output resources. They are, in fact, identical to the following PHP code: <?php define('STDIN', @fopen('php://stdin', 'r')); define('STDOUT', @fopen('php://stdout', 'w')); define('STDERR', @fopen('php://stderr', 'w')); ?> Because they are file references, they can be used with any relevant file-system function to read and write information through your PHP applications. This means that you can write directly to standard error, for instance, by using fputs as shown: <?php fputs(STDERR, "Hello, World!\n"); ?> |