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

Comments

Leave a Reply