Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP PHP Basics Unit Converter Creating a Unit Conversion Tool

Why do I get my calculation from Fahrenheit to Celsius wrong in my database?

//Own example Fahrenheit to Celsius.

//Number in Fahrenheit we want to convert $fahrenheit = 65; // floating point value for the fahrenheit to celcius conversion $fahrenheit_to_celsius = 17.22222; // use the variables to calculate fahrenheit multiplied by the celsius conversion $fahrenheit = $celsius * $fahrenheit_to_celsius; // display the fahrenheit to celsius echo " Calculation = "; echo $fahrenheit; echo " fahrenheit = "; echo $celsius; echo "celsius = ";

Please help - Anybody?

6 Answers

Mary-Ann Burton
Mary-Ann Burton
15,834 Points

I think your code should look something like this (you reversed the fahrenheitand celsius variables in the conversion function ) :

//Number in Fahrenheit we want to convert 
$fahrenheit = 65; 
// floating point value for the fahrenheit to celcius conversion
$fahrenheit_to_celsius = 17.22222; 
// use the variables to calculate fahrenheit multiplied by the celsius conversion 
$celsius = $fahrenheit * $fahrenheit_to_celsius; 
// display the fahrenheit to celsius 
echo " Calculation = "; 
echo $fahrenheit; 
echo " fahrenheit = "; 
echo $celsius; 
echo "celsius ";

but also, the calculation is not 100% accurate ;) The formula is T(°C) = (T(°F) - 32) / 1.8

Appreciate the answer. However, could you explain the last part? I mean the formula? T(C) - 32/1.8 ?

Mary-Ann Burton
Mary-Ann Burton
15,834 Points

To convert celsius to Fahrenheit : $celsius = (($fahrenheit - 32) / 1.8); Should give a more accurate answer

Oleg Kuperman
Oleg Kuperman
2,188 Points

<?php // number in fahrenheit we want to convert to celcius $fahrenheit = 84; // floating point value for the fahrenheit to celcius conversion $f_to_c = 1.8; // use the variable above to calculate fahrenheit minus 32 divided by the celcius conversion $celcius = ($fahrenheit - 32) / $f_to_c; // display the fahrenheit to celcius echo "Temperature: "; echo $fahrenheit; echo " F = "; echo $celcius; echo " C"; ?>

I just did some research and found a simpler way. I don't know if this is how Alena wanted us to format it.

//numbers in fahrenheit we want to convert to celsius
$fahrenheit = 60;
//the fahrenheit to celsius conversion
$celsius = ($fahrenheit - 32) * 5/9;
//display the fahrenheit to celsius 
echo "Temperature: " . $fahrenheit . " Fahrenheit = " . $celsius . " Celsius\n";


//numbers in celsius we want to convert to farenheit 
$celsius = 40;
//the celsius to farenheit conversion
$fahrenheit = ($celsius * 9/5) + 32;
//display the celsius to fahrenheit 
echo "Temperature: " . $celsius . " Celsius = " . $fahrenheit . " Fahrenheit\n";
Alena Holligan
STAFF
Alena Holligan
Treehouse Teacher

There are so many ways to do everything in programming, it's great to experiment with different implementations. The goal for this was just to get you to practice ;) WHOO HOO! Goal accomplished!

Thank you so much for the response!!! It is very assuring to get your feedback and know that we are on the right track by experimenting with different ways! =D Now I feel more encouraged to take risks with coding instead of trying to code only one way. Like you said in your course coding is about finding the most efficient way to do things. I suppose the way in which you do something doesn't matter, as long as it is efficient and gives you the results you want.

Jeremy Antoine
Jeremy Antoine
15,785 Points

You could just be a dork like me and make it way more complicated than it has to be by writing a function for it...

$faren = 98;

//function for the farenheit to celsius conversion. function faren_to_cel($far) { $cel = (($far - 32) * (5/9)); return $cel; }

//call conversion function $cel = faren_to_cel($faren);

//display your findings. echo "When it is $faren degrees Farenheight outdside, it is also $cel degrees celsius.<br />\n";