lzylzlz Posted September 14, 2009 Share Posted September 14, 2009 I'm pretty new to this. I'm trying to get it to display conversions of Celsius to Fahrenheit when the users types in "Celsius" and Fahrenheit to Celsius when the user types in "Fahrenheit" but so far no matter which i type, it displays both instead of just the ONE its suppose to. Any suggestions on what im doing wrong would be great. Heres what I have so far: <?php if ($input == null) {echo "<p>You failed to enter Fahrenheit or Celsius </p>"; } else { if ($input == "farhenheit" or "Farhenheit") echo "<p>Coversion of 0 to 25 degrees Fahrenheit to Celsius: </p>"; for ($i=0, $k =0; $k <=25; $i++, $k++) {$a= round(($i - 32)*5.0/9.0, 2); echo $k . " degrees Farhenheit is equal to " . $a . " degrees Celsius <br>"; } if($input == "celsius" or "Celsius") echo "<p>Conversion of 0 to 25 degrees Celsius to Fahrenheit: </p>"; for ($i=0, $k=0; $k <=25; $i++, $k++) {$b= round($i*9.0/5.0+32, 2); echo $k . " degrees Celsius is equal to " . $b . " degrees Farhenheit <br>"; } } ?> Edited for CODE tags..... Quote Link to comment https://forums.phpfreaks.com/topic/174150-help-with-if-else-statement/ Share on other sites More sharing options...
Philip Posted September 14, 2009 Share Posted September 14, 2009 Proper syntax for the or is: if($var = 'value' OR $var = 'AnotherVal') // or if($var = 'value' || $var = 'AnotherVal') For your case I'd do: if(strtolower($input) == 'fahrenheit') { Also, if ($input == "farhenheit" or "Farhenheit") echo "<p>Coversion of 0 to 25 degrees Fahrenheit to Celsius: </p>"; for ($i=0, $k =0; $k <=25; $i++, $k++) That will only echo for the case, but will always run the for loop... you need to add curly brackets around the if contents Quote Link to comment https://forums.phpfreaks.com/topic/174150-help-with-if-else-statement/#findComment-918025 Share on other sites More sharing options...
lzylzlz Posted September 14, 2009 Author Share Posted September 14, 2009 i added my brackets so now ijust get a blank screen or an error saying unexpected end. Here's a sample of what I did: if (strtolower($input) == 'Fahrenheit') { echo "<p>Coversion of 0 to 25 degrees fahrenheit to Celsius: </p>"; for ($i=0, $k =0; $k <=25; $i++, $k++) {$a= round(($i - 32)*5.0/9.0, 2); echo $k . " degrees fahrenheit is equal to " . $a . " degrees Celsius <br>"; } } Quote Link to comment https://forums.phpfreaks.com/topic/174150-help-with-if-else-statement/#findComment-918039 Share on other sites More sharing options...
Amtran Posted September 14, 2009 Share Posted September 14, 2009 End is usually caused by incorrect syntax with {} (curly braces). Check your code and make sure NONE of it is missing any of {} these guys. On a side note, it would be MUCH easier to just make a radio button dealy instead of making them type in the type of conversion. Quote Link to comment https://forums.phpfreaks.com/topic/174150-help-with-if-else-statement/#findComment-918041 Share on other sites More sharing options...
Philip Posted September 14, 2009 Share Posted September 14, 2009 <?php if(is_null($input)) { echo "<p>You failed to enter Fahrenheit or Celsius </p>"; } else { // using a switch, with all lowercase input switch(strtolower($input)) { case 'farhenheit': echo "<p>Coversion of 0 to 25 degrees Fahrenheit to Celsius: </p>"; for ($i=0, $k =0; $k <=25; $i++, $k++) { $a= round(($i - 32)*5.0/9.0, 2); echo $k . " degrees Farhenheit is equal to " . $a . " degrees Celsius <br>"; } break; case 'celsius': // also the default case.... default: echo "<p>Conversion of 0 to 25 degrees Celsius to Fahrenheit: </p>"; for ($i=0, $k=0; $k <=25; $i++, $k++) { $b= round($i*9.0/5.0+32, 2); echo $k . " degrees Celsius is equal to " . $b . " degrees Farhenheit <br>"; } break; } } ?> Take a look at that. On a side note, it would be MUCH easier to just make a radio button dealy instead of making them type in the type of conversion. True - but you should still check their values to make sure its nothing that could endanger your script. Quote Link to comment https://forums.phpfreaks.com/topic/174150-help-with-if-else-statement/#findComment-918059 Share on other sites More sharing options...
Amtran Posted September 14, 2009 Share Posted September 14, 2009 Yup, it's very important to sanitize EVERYTHNG, especially during flu season. You're right though, make sure you check it before it goes in your DB. Quote Link to comment https://forums.phpfreaks.com/topic/174150-help-with-if-else-statement/#findComment-918070 Share on other sites More sharing options...
grissom Posted September 14, 2009 Share Posted September 14, 2009 Be consistent about your spelling of the word fahrenheit. It's not farhenheit. Quote Link to comment https://forums.phpfreaks.com/topic/174150-help-with-if-else-statement/#findComment-918154 Share on other sites More sharing options...
lzylzlz Posted September 15, 2009 Author Share Posted September 15, 2009 so far nothing has worked. I have to use an if elseif statement in my code. It still just runs the first block of code no matter what I put in the input field on my form. This is what I have: if ($input = 'Fahrenheit' || 'fahrenheit') { echo "<p>Conversion of 0 to 25 degrees Fahrenheit to Celsius: </p>"; for ($i=0, $k =0; $k <=25; $i++, $k++) { $a= round(($i - 32)*5.0/9.0, 2); echo $k . " degrees Fahrenheit is equal to " . $a . " degrees Celsius <br>"; } } elseif($input = 'Celsius' || 'celsius') {echo "<p>Conversion of 0 to 25 degrees Celsius to Fahrenheit: </p>"; for ($i=0, $k=0; $k <=25; $i++, $k++) {$b= round($i*9.0/5.0+32, 2); echo $k . " degrees Celsius is equal to " . $b . " degrees Fahrenheit <br>"; }} else {echo "<p>You failed to enter Fahrenheit or Celsius </p>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/174150-help-with-if-else-statement/#findComment-918681 Share on other sites More sharing options...
Philip Posted September 15, 2009 Share Posted September 15, 2009 Proper syntax for the or is: if($var = 'value' OR $var = 'AnotherVal') // or if($var = 'value' || $var = 'AnotherVal') Quote Link to comment https://forums.phpfreaks.com/topic/174150-help-with-if-else-statement/#findComment-918691 Share on other sites More sharing options...
lzylzlz Posted September 15, 2009 Author Share Posted September 15, 2009 I fixed that but it doesn't solve my problem =( Quote Link to comment https://forums.phpfreaks.com/topic/174150-help-with-if-else-statement/#findComment-919146 Share on other sites More sharing options...
Philip Posted September 15, 2009 Share Posted September 15, 2009 So.... what's your current code? Quote Link to comment https://forums.phpfreaks.com/topic/174150-help-with-if-else-statement/#findComment-919149 Share on other sites More sharing options...
lzylzlz Posted September 15, 2009 Author Share Posted September 15, 2009 I actually JUST figured it out!! i put in == instead of just one = in my if statements. Works perfectly now Quote Link to comment https://forums.phpfreaks.com/topic/174150-help-with-if-else-statement/#findComment-919156 Share on other sites More sharing options...
Philip Posted September 15, 2009 Share Posted September 15, 2009 Oops, my bad typo sorry Quote Link to comment https://forums.phpfreaks.com/topic/174150-help-with-if-else-statement/#findComment-919161 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.