stockton Posted February 5, 2010 Share Posted February 5, 2010 Please tell me what stupid mistake I have made in the following code. <?php $operator = fgets(STDIN); $operator = $operator[0]; echo "\n".$operator."\n"; switch ($operator) { case '+': echo "Plus"; break; case '-': echo "Minus"; break; } ?> The switch never finds a case for either operator. Why? Quote Link to comment https://forums.phpfreaks.com/topic/191053-case-statement-failing/ Share on other sites More sharing options...
trq Posted February 5, 2010 Share Posted February 5, 2010 Obviously because $operator never equals + or - Quote Link to comment https://forums.phpfreaks.com/topic/191053-case-statement-failing/#findComment-1007407 Share on other sites More sharing options...
stockton Posted February 5, 2010 Author Share Posted February 5, 2010 Not too obvious to me and that was why I did the echo. echo "\n".$operator."\n" To see what it contained. It prints the plus sign. Quote Link to comment https://forums.phpfreaks.com/topic/191053-case-statement-failing/#findComment-1007415 Share on other sites More sharing options...
n3ssi3 Posted February 5, 2010 Share Posted February 5, 2010 Have you tried with trim() ? maybe you have a space or something in $operator ... but if i set $operator = array('-','+'); it works nothing wrong with you're switch case Quote Link to comment https://forums.phpfreaks.com/topic/191053-case-statement-failing/#findComment-1007416 Share on other sites More sharing options...
stockton Posted February 5, 2010 Author Share Posted February 5, 2010 However if the user types the + or - on the keyboard it fails. Quote Link to comment https://forums.phpfreaks.com/topic/191053-case-statement-failing/#findComment-1007421 Share on other sites More sharing options...
n3ssi3 Posted February 5, 2010 Share Posted February 5, 2010 However if the user types the + or - on the keyboard it fails. macbookpro:~ phptest$ php test.php + + Plus macbookpro:~ phptest$ php test.php - - Minus sry but it works for me...even with STDIN ... Have you other code in the script ? maybe there is the error... Quote Link to comment https://forums.phpfreaks.com/topic/191053-case-statement-failing/#findComment-1007430 Share on other sites More sharing options...
oni-kun Posted February 5, 2010 Share Posted February 5, 2010 However if the user types the + or - on the keyboard it fails. <?php $operator = fgets(STDIN); echo var_dump ($operator) . "<br/>\n"; echo ord(trim($operator[0])); ?> Please post the results of this. What you are most likely getting is something encoded, or as mentioned an untrimmed character. This will verify it 'bit by bit' to prove your point. Quote Link to comment https://forums.phpfreaks.com/topic/191053-case-statement-failing/#findComment-1007437 Share on other sites More sharing options...
stockton Posted February 5, 2010 Author Share Posted February 5, 2010 The following now works but I don't understand what changed to make it work. <?php $operator = fgets(STDIN); $operator = $operator[0]; echo var_dump ($operator) . "<br/>\n"; echo ord(trim($operator[0])); // echo "\n".$operator."\n"; switch ($operator) { case '+': echo "\nPlus\n"; break; case '-': echo "\nMinus\n"; break; } ?> the result of the above is:- php Switch.php + string(1) "+" <br/> 43 Plus Quote Link to comment https://forums.phpfreaks.com/topic/191053-case-statement-failing/#findComment-1007477 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.