Created
January 31, 2017 23:11
-
-
Save vielhuber/bd95b79166963800e6b60accdd7692f9 to your computer and use it in GitHub Desktop.
PHP-cli ask user for command line input #php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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