Jump to content

i need some help


zafira
Go to solution Solved by hansford,

Recommended Posts

Write a program which can convert a temperature in Fahrenheit to the corresponding temperature in Celcius and vice versa. To convert a temperature in Fahrenheit to Celsius, 32 degrees need to be subtracted and the result needs to be multiplied by (5/9). To convert a temperature in Celsius to Fahrenheit, divide the temperature by (5/9) and add 32 degrees to the result.

 

this my coding

 

<html>
<head>
<title>convert</title>
</head>
<body>
 
<?php
//Read the temperature 
 
$temp = "" ;
 
//Read the 'direction' that is selected -- from Fahrentheit to Celcius ('ftc') or from Celcius to Fahrentheit ('clt').
 
 
$direction = "" ;
 
if($direction == "ftc")
{
//Include the calculations that are needed.
 
echo " $temp degrees Fahrentheit is <b>$temp_new</b> degrees Celsius " ; 
}
else
{
//Include the calculations that are needed.
 
echo " $temp degrees Celcius is <b>$temp_new</b> degrees Fahrentheit " ; 
}
 
?>
<form action="convert.php" method="post">
<select name="direction">
<option value="ftc">Fahrenheit to Celsius </option>
<option value="ctf">Celsius to Fahrenheit </option>
</select>
<input type="text" name="temp" size="6"/> <br/>
<input type="Submit" value="Convert"/>
</form>
</body>
</html>
Edited by Ch0cu3r
Link to comment
Share on other sites

  • Solution

I just set it up on the same page for illustration purposes. 

<?php
error_reporting(E_ALL);
ini_set('display_errors',1);

if (isset($_POST['direction']) && !empty($_POST['temp']))
{
    $direction = $_POST['direction'];
    $temp = $_POST['temp'];
    
    if ( ! is_numeric($temp))
    {
        $error[] = "Input is not a valid number.";
    }
    
    if ( ! isset($error))
    {
        if ($direction == "ftc")
        { 
            $temp_new = convert_temp($temp);
            echo "$temp degrees Fahrentheit is <b>$temp_new</b> degrees Celsius " ;
        }
        else
        {
            $temp_new = convert_temp($temp,true);
            echo "$temp degrees Celcius is <b>$temp_new</b> degrees Fahrentheit " ;
        }
    }
    else 
    {
        foreach ($error as $err)
        {
            echo $err . '<br />';
        }
    }
}

function convert_temp($temp, $celcius=false)
{
    if ($celcius)
    {
        $ret = ($temp * 9/5) + 32;
    }
    else 
    {
        $ret = ($temp - 32) * 5/9;
    }
    
    return round($ret,1);
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>convert</title>
</head>
<body>
<form action="" method="post">
<select name="direction">
<option value="ftc" selected>Fahrenheit to Celsius </option>
<option value="ctf">Celsius to Fahrenheit </option>
</select>
<input type="text" name="temp" size="6"/> <br/>
<input type="Submit" value="Convert"/>
</form>
</body>
</html>

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.