(PHP 5 >= 5.1.0, PHP 7, PHP 8)
readline_callback_handler_install — 初始化一個(gè) readline 回調(diào)接口,然后終端輸出提示信息并立即返回
設(shè)置一個(gè) readline 回調(diào)接口然后輸出 prompt
并立即返回.
第二次調(diào)用這個(gè)函數(shù)不需要移除上一個(gè)回調(diào)接口,這個(gè)函數(shù)將自動(dòng)覆蓋舊的接口.
當(dāng)配合 stream_select() 時(shí)回調(diào)的特性非常有用,它允許在 IO 與用戶輸入 間交叉進(jìn)行,不像readline().
prompt
提示信息.
callback
callback
函數(shù)需要一個(gè)參數(shù); 用戶輸入將被返回.
成功時(shí)返回 true
, 或者在失敗時(shí)返回 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";
?>