Search code examples
php

PHP CLI getting input from user and then dumping into variable possible?


Is it possible to get input from a user using PHP CLI, then dump the input into a variable and then the script goes ahead. Just like the C++ cin function.

Is that possible and, if yes, then how?

Maybe not only PHP but maybe with some Linux commands?


Solution

  • Have a look at this PHP manual page http://php.net/manual/en/features.commandline.php

    in particular

    <?php
    echo "Are you sure you want to do this?  Type 'yes' to continue: ";
    $handle = fopen ("php://stdin","r");
    $line = fgets($handle);
    if(trim($line) != 'yes'){
        echo "ABORTING!\n";
        exit;
    }
    echo "\n";
    echo "Thank you, continuing...\n";
    ?>