PHP Command Line Confirmation Prompt

Get user confirmation before continuing in PHP script.

<?php
echo 'Continue? [y/n] ';
 
if (!in_array(trim(fgets(STDIN)), array('y', 'Y'))) {
    echo 'Stopped' . "\n";
    exit;
}
 
echo 'Continuing' . "\n";

Examples

$ php confirm.php 
Continue? [y/n] n
Stopped
$ php confirm.php 
Continue? [y/n] y
Continuing
Did this help you? Please let me know with a comment. Thank you
Posted .

Comments (RSS)

Leave a comment