0

My code looks like this :

switch ($input)
{
case "hello":
    .......code1..... ;
     break;
case "bye" :
     ......code2 .....;
     break;
case "xoxo":
     .......code3.....;
     break;
default:
    .......code4.....;

}

And I am using $input=fgets(STDIN) to take the input but it doesn't execute the respective codes even if the case is matched. A var_dump($input); or an echo $input; shows the value is assigned correctly (at least it looks to me). What do i need to add (or modify) in the codes ?

Just Khaithang
  • 1,217
  • 1
  • 14
  • 24

1 Answers1

0

$input=fgets(STDIN) contains \n new line character at the end. Compare the two var dumps from this code:

$input=fgets(STDIN);
var_dump($input);
$input = rtrim($input);
var_dump($input);

and the output called with $ echo 'hello' | php a.php

string(6) "hello
"
string(5) "hello"

First one is 6 characters long: Hello + new line character, second one is exprected 5 characters long 'hello' string.

blahy
  • 1,294
  • 1
  • 8
  • 9