Skip to content

Instantly share code, notes, and snippets.

@bricejulia
Forked from j3j5/prompt_answer.php
Created July 13, 2016 08:14
Show Gist options
  • Save bricejulia/41aa1a562bd93e2d807f7219b2c6e844 to your computer and use it in GitHub Desktop.
Save bricejulia/41aa1a562bd93e2d807f7219b2c6e844 to your computer and use it in GitHub Desktop.
PHP snippet to prompt for an answer on the CLI.
function prompt_answer($exit = TRUE) {
$handle = fopen ("php://stdin","r");
$line = fgets($handle);
if(trim($line) !== 'y' && !empty(trim($line))) {
fclose($handle);
if($exit) {
echo "ABORTING!" . PHP_EOL;
exit;
} else {
return FALSE;
}
}
if(!$exit) {
return TRUE;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment