PHP のコマンド ラインから一度に 1 文字ずつ読み取りたいのですが、どこかからの何らかの入力バッファリングがこれを妨げているようです。
次のコードを検討してください。
#!/usr/bin/php
<?php
echo "input# ";
while ($c = fread(STDIN, 1)) {
echo "Read from STDIN: " . $c . "\ninput# ";
}
?>
入力として「foo」と入力して(Enterキーを押して)、得られる出力は次のとおりです。
input# foo
Read from STDIN: f
input# Read from STDIN: o
input# Read from STDIN: o
input# Read from STDIN:
input#
私が期待している出力は次のとおりです。
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#
(つまり、入力時に文字が読み取られ、処理されます)。
ただし、現在、各文字は、Enter キーが押された後にのみ読み取られます。TTY が入力をバッファリングしている疑いがあります。
最終的には、上矢印、下矢印などのキー押下を読み取れるようにしたい.