Jump to content

Help with If else statement


lzylzlz

Recommended Posts

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.....

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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>";

        } }

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

<?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.

 

Link to comment
Share on other sites

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>"; }

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.