NeverPhased
NeverPhased

Reputation: 1566

User interaction with PHP via command line skipping an input

I have 3 identical snippets which ask the user for an input via command line. The first query is asked to the user and the third but for some reason the 2nd snippet is not outputting properly although its identical code. Below is the output:

enter image description here

As you can see it skips content 2 and runs straight to content 3. Any ideas what is going wrong?

As soon as I hit y for content 1 the code runs straight to Would you like to print content 3!

<?php

echo "\n\033[1;35m~~~~~~ CONTENT 1 ~~~~~~\033[0m\n\n";

echo "\033[1;37mWould you like to print content 1? (y/n) - ";
$stdin = fopen('php://stdin', 'r');
$response = fgetc($stdin);
    if ($response == 'y') {
      echo "\033[0m";
      echo "Content 1 print success!";
    }

echo "\n\033[1;35m~~~~~~ CONTENT 2 ~~~~~~\033[0m\n\n"; 

echo "\033[1;37mWould you like to print content 2? (y/n) - ";
$stdin = fopen('php://stdin', 'r');
$response = fgetc($stdin);
    if ($response == 'y') {
      echo "\033[0m";
      echo "Content 2 print success!";

    }

echo "\n\033[1;35m~~~~~~ CONTENT 3 ~~~~~~\033[0m\n\n"; 

echo "\033[1;37mWould you like to print content 3? (y/n) - ";
$stdin = fopen('php://stdin', 'r');
$response = fgetc($stdin);
            if ($response == 'y') {
              echo "\033[0m";
              echo "Content 3 print success!\n";
            }
echo "\033[0m";

?>

Using the do / while suggestion it allows me to input 3 times but this time for content 2 and 3 the request to input is duplicated!

See here:

    <?

    echo "\n\033[1;35m~~~~~~ CONTENT 1 ~~~~~~\033[0m\n\n";

    do {
        echo "\033[1;37mWould you like to print content 1? (y/n) - ";
        $stdin = fopen('php://stdin', 'r');

        $response = fgetc($stdin);
    } while (!in_array($response, ['y','n']));
    if ($response == 'y') {
      echo "\033[0m";
      echo "Content 1 print success!";
    }

    echo "\n\033[1;35m~~~~~~ CONTENT 2 ~~~~~~\033[0m\n\n";

    do {
        echo "\033[1;37mWould you like to print content 2? (y/n) - ";
        $stdin = fopen('php://stdin', 'r');

        $response = fgetc($stdin);
    } while (!in_array($response, ['y','n']));
    if ($response == 'y') {
      echo "\033[0m";
      echo "Content 2 print success!";
    }

    echo "\n\033[1;35m~~~~~~ CONTENT 3 ~~~~~~\033[0m\n\n";

    do {
        echo "\033[1;37mWould you like to print content 3? (y/n) - ";
        $stdin = fopen('php://stdin', 'r');

        $response = fgetc($stdin);
    } while (!in_array($response, ['y','n']));
    if ($response == 'y') {
      echo "\033[0m";
      echo "Content 3 print success!\n";
    }


    ?>

enter image description here

Could it because the input has some white space or its capturing a newline? Sorry I'm just blindly guessing to be honest...

----- UPDATE -----

I can get my code to work by putting an additional if statment in to force the echo to print only once. Its a hack but if anyone can come up with a better solution please let me know!

    echo "\n\033[1;35m~~~~~~ CONTENT 3 ~~~~~~\n\n";
    $x = "1";
    do {
        if ($x==1){
          echo "\033[1;37mWould you like to print content 2? (y/n) - \033[0m\n";
          $x = $x+1;

        }
        $stdin = fopen('php://stdin', 'r');

        $response = fgetc($stdin);
    } while (!in_array($response, ['y','n']));
    if ($response == 'y') {
      echo "\033[0m";
      echo "Content 3 print success!\n";
    }

Upvotes: 2

Views: 273

Answers (1)

Felippe Duarte
Felippe Duarte

Reputation: 15131

Your code works here. Anyway, if you want to get valid inputs, use this do/while, to just accept Y or N as input:

echo "\n\033[1;35m~~~~~~ CONTENT 1 ~~~~~~\033[0m\n\n";

do {
    echo "\033[1;37mWould you like to print content 1? (y/n) - ";
    $stdin = fopen('php://stdin', 'r');

    $response = fgetc($stdin);
} while (!in_array($response, ['y','n']));
if ($response == 'y') {
  echo "\033[0m";
  echo "Content 1 print success!";
}

Upvotes: 1

Related Questions