readline_callback_handler_install
(PHP 5 >= 5.1.0, PHP 7, PHP 8)
readline_callback_handler_install — 初始化一個 readline 回撥介面,然後終端輸出提示資訊並立即返回
說明
設定一個 readline 回撥介面然後輸出 prompt
並立即返回.
第二次呼叫這個函式不需要移除上一個回撥介面,這個函式將自動覆蓋舊的介面.
當配合 stream_select() 時回撥的特性非常有用,它允許在 IO 與使用者輸入 間交叉進行,不像readline().
參數
-
prompt
-
提示資訊.
-
callback
-
callback
函式需要一個參數; 使用者輸入將被返回.
返回值
成功時返回 true
, 或者在失敗時返回 false
。
範例
示例 #1 Readline Callback Interface Example
<?php
function rl_callback($ret)
{
global $c, $prompting;
echo "You entered: $ret\n";
$c++;
if ($c > 10) {
$prompting = false;
readline_callback_handler_remove();
} else {
readline_callback_handler_install("[$c] Enter something: ", 'rl_callback');
}
}
$c = 1;
$prompting = true;
readline_callback_handler_install("[$c] Enter something: ", 'rl_callback');
while ($prompting) {
$w = NULL;
$e = NULL;
$n = stream_select($r = array(STDIN), $w, $e, null);
if ($n && in_array(STDIN, $r)) {
// read a character, will call the callback when a newline is entered
readline_callback_read_char();
}
}
echo "Prompting disabled. All done.\n";
?>
參見
- readline_callback_handler_remove() - 移除上一個安裝的回撥函式控制代碼並且恢復終端設定
- readline_callback_read_char() - 當一個行被接收時讀取一個字元並且通知 readline 呼叫回撥函式
- stream_select() - Runs the equivalent of the select() system call on the given arrays of streams with a timeout specified by seconds and microseconds