title: PHP CLI: How to read a single character of input from the TTY (without waiting for the enter key)?
body: <p>I want to read a single character at-a-time from the command line in PHP, however it seems as though there is some kind of input buffering from somewhere preventing this.</p>
  
  <p>Consider this code:</p>
  
  <pre><code>#!/usr/bin/php
  &lt;?php
  echo "input# ";
  while ($c = fread(STDIN, 1)) {
      echo "Read from STDIN: " . $c . "\ninput# ";
  }
  ?&gt;
  </code></pre>
  
  <p>Typing in "foo" as the input (and pressing enter), the output I am getting is:</p>
  
  <pre><code>input# foo
  Read from STDIN: f
  input# Read from STDIN: o
  input# Read from STDIN: o
  input# Read from STDIN: 
  
  input# 
  </code></pre>
  
  <p>The output I am <em>expecting</em> is:</p>
  
  <pre><code>input# f
  input# Read from STDIN: f
  
  input# o
  input# Read from STDIN: o
  
  input# o
  input# Read from STDIN: o
  
  input# 
  input# Read from STDIN: 
  
  input# 
  </code></pre>
  
  <p>(That is, with characters being read and processed as they are typed).</p>
  
  <p>However, currently, each character is being read only after enter is pressed. I have a suspicion the TTY is buffering the input.</p>
  
  <p>Ultimately I want to be able to read keypresses such as UP arrow, DOWN arrow, etc.</p>