Valda Posted February 9, 2014 Share Posted February 9, 2014 Howdy folks! Please go easy on me, I'm relatively new to programming and still climbing through the basics. My code is pretty simple. User gotta pick "d" or "w" to deposit or withdraw his fake virtual money, then he picks the amount and the awesomely cool calculation transpires But it doesn't work. No matter what is being picked the keyboard always goes to the "else" condition (which tells the user "You can only use enter 'w' or 'd'" -- despite the fact I input "d" or "w"). Basically it completely skips my "if" and "else if" statements. I tried putting "d" in quotations, without, with tags, with what not! So, any clue what is being done wrong here? <?php $balance=1000; echo "Welcome to your bank. Your balance is " . $balance . " Which action would you like to take? (w=withdraw, d=deposit) \n" ; $user=fgets(STDIN); if ($user=="w") { echo "How much would you like to withdraw? \n"; (int)$sum=fgets(STDIN); $balance = $balance-$sum; echo "Money successfully withdrawn. Your balance now stands at" . $balance . ""; } else if ($user=="d") { echo "How much would you like to deposit? \n"; $sum=fgets(STDIN); $balance = $balance+$sum; echo "Money successfully deposited. Your balance now stands at" . $balance . "" ; } else { echo "You can only use enter 'w' or 'd'"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/286055-newbie-question-getting-the-user-to-pick-an-option-through-a-keboard-key-is-being-ignored/ Share on other sites More sharing options...
Valda Posted February 9, 2014 Author Share Posted February 9, 2014 Two things I wanted to add, first is that I'm running it in Command Prompt Secondly that changing fgets to fgetc allowed me to define $user as string, however it completely skips "(int)$sum=fgets(STDIN);" Why? Quote Link to comment https://forums.phpfreaks.com/topic/286055-newbie-question-getting-the-user-to-pick-an-option-through-a-keboard-key-is-being-ignored/#findComment-1468257 Share on other sites More sharing options...
ginerjm Posted February 9, 2014 Share Posted February 9, 2014 (edited) According to a google search you might try this: $stdin = fopen('php://stdin', 'r'); $response = fgets($stdin); PS - it is NOT completely skipping your if/else statements. It is logically proceeding through them and coming to a completely anticipated conclusion. Edited February 9, 2014 by ginerjm Quote Link to comment https://forums.phpfreaks.com/topic/286055-newbie-question-getting-the-user-to-pick-an-option-through-a-keboard-key-is-being-ignored/#findComment-1468276 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.