<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Input/output streams</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="features.commandline.usage.html">Usage</a></div> <div class="next" style="text-align: right; float: right;"><a href="features.commandline.interactive.html">Interactive shell</a></div> <div class="up"><a href="features.commandline.html">Command line usage</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="features.commandline.io-streams" class="section"> <h2 class="title">Input/output streams</h2> <p class="para"> The <acronym title="Command Line Interpreter/Interface">CLI</acronym> <acronym title="Server Application Programming Interface">SAPI</acronym> defines a few constants for I/O streams to make programming for the command line a bit easier. </p> <p class="para"> <table class="doctable table"> <caption><strong>CLI specific Constants</strong></caption> <thead> <tr> <th>Constant</th> <th>Description</th> </tr> </thead> <tbody class="tbody"> <tr> <td><strong><code>STDIN</code></strong></td> <td> <p class="para">An already opened stream to <em>stdin</em>. This saves opening it with <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />$stdin </span><span style="color: #007700">= </span><span style="color: #0000BB">fopen</span><span style="color: #007700">(</span><span style="color: #DD0000">'php://stdin'</span><span style="color: #007700">, </span><span style="color: #DD0000">'r'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> If you want to read single line from <em>stdin</em>, you can use <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />$line </span><span style="color: #007700">= </span><span style="color: #0000BB">trim</span><span style="color: #007700">(</span><span style="color: #0000BB">fgets</span><span style="color: #007700">(</span><span style="color: #0000BB">STDIN</span><span style="color: #007700">)); </span><span style="color: #FF8000">// reads one line from STDIN<br /></span><span style="color: #0000BB">fscanf</span><span style="color: #007700">(</span><span style="color: #0000BB">STDIN</span><span style="color: #007700">, </span><span style="color: #DD0000">"%d\n"</span><span style="color: #007700">, </span><span style="color: #0000BB">$number</span><span style="color: #007700">); </span><span style="color: #FF8000">// reads number from STDIN<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </p></td> </tr> <tr> <td><strong><code>STDOUT</code></strong></td> <td><p class="para"> An already opened stream to <em>stdout</em>. This saves opening it with <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />$stdout </span><span style="color: #007700">= </span><span style="color: #0000BB">fopen</span><span style="color: #007700">(</span><span style="color: #DD0000">'php://stdout'</span><span style="color: #007700">, </span><span style="color: #DD0000">'w'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </p></td> </tr> <tr> <td><strong><code>STDERR</code></strong></td> <td> <p class="para"> An already opened stream to <em>stderr</em>. This saves opening it with <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />$stderr </span><span style="color: #007700">= </span><span style="color: #0000BB">fopen</span><span style="color: #007700">(</span><span style="color: #DD0000">'php://stderr'</span><span style="color: #007700">, </span><span style="color: #DD0000">'w'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </p> </td> </tr> </tbody> </table> </p> <p class="para"> Given the above, you don't need to open e.g. a stream for <em>stderr</em> yourself but simply use the constant instead of the stream resource: <div class="example-contents"> <div class="shellcode"><pre class="shellcode">php -r 'fwrite(STDERR, "stderr\n");'</pre> </div> </div> You do not need to explicitly close these streams, as they are closed automatically by PHP when your script ends. </p> <blockquote class="note"><p><strong class="note">Note</strong>: <p class="para"> These constants are not available if reading the PHP script from <em>stdin</em>. </p> </p></blockquote> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="features.commandline.usage.html">Usage</a></div> <div class="next" style="text-align: right; float: right;"><a href="features.commandline.interactive.html">Interactive shell</a></div> <div class="up"><a href="features.commandline.html">Command line usage</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>