The CLI SAPI defines a few constants for I/O streams to make programming for the command line a bit easier.
STDIN
An already opened stream to stdin. This saves opening it with
stdin
<?php $stdin = fopen('php://stdin', 'r'); ?>
<?php $line = trim(fgets(STDIN)); // reads one line from STDIN fscanf(STDIN, "%d\n", $number); // reads number from STDIN ?>
STDOUT
An already opened stream to stdout. This saves opening it with
stdout
<?php $stdout = fopen('php://stdout', 'w'); ?>
STDERR
An already opened stream to stderr. This saves opening it with
stderr
<?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");'
Note: These constants are not available if reading the PHP script from stdin.
Note:
These constants are not available if reading the PHP script from stdin.