Table 23-1. Overridden php.ini directives
Directive CLI SAPI default value Comment
html_errors FALSE It can be quite hard to read the error
message in your shell when it's cluttered with all those
meaningless HTML tags, therefore this directive defaults
to FALSE.
implicit_flush TRUE It is desired that any output coming
from print(), echo() and friends is immediately written
to the output and not cached in any buffer. You still can
use output buffering if you want to defer or manipulate
standard output.
max_execution_time 0 (unlimited) Due to endless possibilities
of using PHP in shell environments, the maximum execution
time has been set to unlimited. Whereas applications written
for the web are often executed very quickly, shell application
tend to have a much longer execution time.
register_argc_argv TRUE Because this setting is TRUE you
will always have access to argc (number of arguments passed
to the application) and argv (array of the actual arguments)
in the CLI SAPI.
As of PHP 4.3.0, the PHP variables $argc and $argv are
registered and filled in with the appropriate values when
using the CLI SAPI. Prior to this version, the creation
of these variables behaved as they do in CGI and MODULE
versions which requires the PHP directive register_globals
to be on. Regardless of version or register_globals setting,
you can always go through either $_SERVER or $HTTP_SERVER_VARS.
Example: $_SERVER['argv']
Note: These directives cannot be initialized with another
value from the configuration file php.ini or a custom one
(if specified). This is a limitation because those default
values are applied after all configuration files have been
parsed. However, their value can be changed during runtime
(which does not make sense for all of those directives,
e.g. register_argc_argv).
To ease working in the shell environment, the following
constants are defined:
Table 23-2. CLI specific Constants
Constant Description
STDIN An already opened stream to stdin. This saves opening
it with <?php
$stdin = fopen('php://stdin', 'r');
?>
STDOUT An already opened stream to stdout. This saves opening
it with <?php
$stdout = fopen('php://stdout', 'w');
?>
STDERR An already opened stream to stderr. This saves opening
it with <?php
$stderr = fopen('php://stderr', 'w');
?>
Given the above, you don't need to open e.g. a stream for
stderr yourself but simply use the constant instead of the
stream resource: php -r 'fwrite(STDERR, "stderr\n");'
You do not need to explicitly close these streams, as they
are closed automatically by PHP when your script ends.