Team LiB
Previous Section Next Section

Core CLI Differences

When 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.

Table 17.1. Command-Line PHP Arguments


Syntax-highlight and display the provided PHP code (using HTML tags).


Display the provided PHP code with all comments and unnecessary whitespace removed.

-f <filename>

Execute <filename> as a PHP script.


Display PHP's version number.

-c <path>

Set the path to search for php.ini to <path>.


Run PHP interactively, meaning that commands will be executed immediately as they are received instead of parsing the entire script at once.

-d <name>[=[<value>]]

Override a configuration directive named <name> with the value <value>. Omitting just the <value> portion (only an equal sign) sets the directive to nothing. Omitting both the <value> and the equal sign sets the configuration directive to TRue.


Generate extra information used for debugging PHP (the engine itself, not PHP scripts).

-z <filename>

Load the Zend extension <filename>. The default library path will be searched for the file unless an explicit path is provided.

-r <string>

Execute <string> as PHP code. The code provided does not need opening or closing PHP tags (such as <?php ?>).


Check the provided PHP file to see whether it has proper syntax only. PHP will return a message indicating the success of the syntax check and return 0 or 255 indicating whether the code was valid or invalid, respectively.

Note: This does not work with the -r parameter.


Display a list of modules compiled into PHP.


Returns the PHP information HTML document. This is the same as using the phpinfo() function.


Display a short help menu outlining all the accepted arguments available to PHP.

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:


HTML errors are disabled in the CLI version of PHP.


Because of the nature of command-line scripting, PHP will always implicitly flush text to the terminal window.


No maximum execution time limit has been placed on scripts executed with the CLI version of PHP.


The CLI version of PHP always registers the $argc and $argv variables.

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:

    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!


Unfortunately, if you are using PHP in a Windows environment, you cannot use the preceding code to eliminate the need to call PHP automatically to execute your scripts. An alternative solution is to create a batch file as shown (where each parameter %1, %2, and so on represents a parameter passed through to the PHP script):

@C:\PHP\php.exe myscript.php %1 %2 %3 %4 %5

This batch file can then be saved as myscript.bat and executed as needed. Also note that when you're running a Win32 version of PHP, the first #!/usr/local/bin/php line will be ignored. Therefore, all PHP scripts should include a similar line for cross-compatibility reasons.

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:

     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:

    fputs(STDERR, "Hello, World!\n");

    Team LiB
    Previous Section Next Section