Dark Launch

PHP Command Line Confirmation Prompt

Get user confirmation before continuing in PHP script.

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

Examples

Bash
$ php confirm.php 
Continue? [y/n] n
Stopped
Bash
$ php confirm.php 
Continue? [y/n] y
Continuing