Created
June 21, 2011 04:34
-
-
Save jachin/1037249 to your computer and use it in GitHub Desktop.
PHP Command Line Confirmation Prompt
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
$message = "Are you sure you want to do this [y/N]"; | |
print $message; | |
flush(); | |
ob_flush(); | |
$confirmation = trim( fgets( STDIN ) ); | |
if ( $confirmation !== 'y' ) { | |
// The user did not say 'y'. | |
exit (0); | |
} |
here my modification
/**
* Prompts the user for confirmation with a message.
*
* @param string $message The confirmation message.
* @return bool True if user confirms (y/yes), false otherwise (n/no).
*/
function confirmAction(string $message = "Are you sure? (y/n): "): bool
{
$validResponses = ['y', 'yes', 'n', 'no'];
$response = '';
while (!in_array($response, $validResponses)) {
echo $message;
$response = strtolower(trim(fgets(STDIN)));
}
return in_array($response, ['y', 'yes']);
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Very nice, thank you for this helpful snippet.