<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/features.commandline.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'en',
  ),
  'this' => 
  array (
    0 => 'features.commandline.io-streams.php',
    1 => 'I/O streams',
    2 => 'Input/output streams',
  ),
  'up' => 
  array (
    0 => 'features.commandline.php',
    1 => 'Command line usage',
  ),
  'prev' => 
  array (
    0 => 'features.commandline.usage.php',
    1 => 'Usage',
  ),
  'next' => 
  array (
    0 => 'features.commandline.interactive.php',
    1 => 'Interactive shell',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'features/commandline.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="features.commandline.io-streams" class="section">
  <h2 class="title">Input/output streams</h2>
  
  
  <p class="para">
   The <abbr title="Command Line Interpreter/Interface">CLI</abbr> <abbr title="Server Application Programming Interface">SAPI</abbr> 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><a href="reserved.constants.php#constant.stdin">STDIN</a></code></strong></td>
       <td>
        <p class="para">An already opened stream to <code class="literal">stdin</code>. This saves
       opening it with
       <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?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">?&gt;</span></span></code></div>
       </div>

       If you want to read single line from <code class="literal">stdin</code>, you can
       use
       <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?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">?&gt;</span></span></code></div>
       </div>

       </p></td>
      </tr>

      <tr>
       <td><strong><code><a href="reserved.constants.php#constant.stdout">STDOUT</a></code></strong></td>
       <td><p class="para">
       An already opened stream to <code class="literal">stdout</code>. This saves
       opening it with
       <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?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">?&gt;</span></span></code></div>
       </div>

       </p></td>
      </tr>

      <tr>
       <td><strong><code><a href="reserved.constants.php#constant.stderr">STDERR</a></code></strong></td>
       <td>
        <p class="para">
         An already opened stream to <code class="literal">stderr</code>.
         This saves opening it with
         <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?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">?&gt;</span></span></code></div>
         </div>

        </p>
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
  
  <p class="para">
   Given the above, you don&#039;t need to open e.g. a stream for
   <code class="literal">stderr</code> yourself but simply use the constant instead of
   the stream resource:
   <div class="example-contents">
<div class="shellcode"><pre class="shellcode">php -r &#039;fwrite(STDERR, &quot;stderr\n&quot;);&#039;</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
    <code class="literal">stdin</code>.
   </p>
  </p></blockquote>
 </div><?php manual_footer($setup); ?>