command line interface - PHP CLI - Ask for User Input or Perform Action after a Period of Time -
i trying create php script, ask user select option: like:
echo "type number of choice below:"; echo " 1. perform action 1"; echo " 2. perform action 2"; echo " 3. perform action 3 (default)"; $menuchoice = read_stdin(); if ( $menuchoice == 1) { echo "you picked 1"; } elseif ( $menuchoice == 2) { echo "you picked 2"; } elseif ( $menuchoice == 3) { echo "you picked 3"; }
this works nicely 1 can perform actions based on user input.
but expand if user does not type within 5 seconds, default action run automatically without further action user.
is @ possible php...? unfortunately beginner on subject.
any guidance appreciated.
thanks,
hernando
you can use stream_select()
that. here comes example.
echo "input ... (5 sec)\n"; // file descriptor stdin $fd = fopen('php://stdin', 'r'); // prepare arguments stream_select() $read = array($fd); $write = $except = array(); // don't care $timeout = 5; // wait maximal 5 seconds input if(stream_select($read, $write, $except, $timeout)) { echo "you typed: " . fgets($fd) . php_eol; } else { echo "you typed nothing\n"; }
Comments
Post a Comment