Skip to content

Instantly share code, notes, and snippets.

@vielhuber
Created January 31, 2017 23:11
Show Gist options
  • Save vielhuber/bd95b79166963800e6b60accdd7692f9 to your computer and use it in GitHub Desktop.
Save vielhuber/bd95b79166963800e6b60accdd7692f9 to your computer and use it in GitHub Desktop.
PHP-cli ask user for command line input #php
<?php
function ask($question, $answers = null) {
echo $question.' ';
$handle = fopen("php://stdin","r");
$answer = fgets($handle);
fclose($handle);
$answer = trim($answer);
if($answers != null) {
if( !in_array($answer,$answers) ) {
return ask($question, $answers);
}
}
return $answer;
}
if( ask('are you an idiot? (y/n)',['y','n']) == 'y' ) {
die();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment