Become Zend Certified

Prepare for the ZCE exam using our quizzes (web or iPad/iPhone). More info...

When you're ready get 7.5% off your exam voucher using voucher CJQNOV23 at the Zend Store

Input/output streams

The CLI SAPI defines a few constants for I/O streams to make programming for the command line a bit easier.

CLI specific Constants
Constant Description

An already opened stream to stdin. This saves opening it with

If you want to read single line from stdin, you can use
trim(fgets(STDIN)); // reads one line from STDIN
fscanf(STDIN"%d\n"$number); // reads number from STDIN


An already opened stream to stdout. This saves opening it with



An already opened stream to stderr. This saves opening it with


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.


These constants are not available if reading the PHP script from stdin.

PHP Manual