Temperature Conversions Script

pein87's Avatar author of Temperature Conversions Script
This is an article on Temperature Conversions Script in PHP.
This is temperature conversion script, used to convert temperature from Celsius, Fahrenheit, Rankine, and Kelvin.

The Code



Include the script in your php project and call one of the conversion functions with a numeric value as its parameter to get some output. You can convert from Celsius, Fahrenheit, Rankine, and Kelvin temperature types. The functions all except 2 parameters the value(int), and round(true). Because if round does not equal true it will not round but if it does equal true it will round the value using the built in round function.

I've tested the math invloved to make sure the out puts are correct. I did not use the float values because I would have to include ceil and floor options which I felt weren't needed but if you check out this wiki page you can try your hand at the conversions there(I used them as my source) http://en.wikipedia.org/wiki/Rankine_scale

Code: Php
<?php

$NaN = "NaN";

/* Celsius to and from Fahrenheit conversions */

function fahrenheitToCelsius($far,$round="true")
{

    global $NaN;   
    if(!is_numeric($far))
    {
        return $NaN;
    }
    else
    {
        $CelsiusVal = 5 * ($far - 32) / 9
        if($round == "true")
        {
            return round($CelsiusVal)
        }
        else
        {
            return $CelsiusVal;
        }
    }
}

function celsiusToFahrenheit($cel, $round="true")
{
    global $NaN;
    if(!is_numeric($cel))
    {
        return $NaN;
    }
    else
    {
        $FahrenheitVal = $cel * 9 / 5 + 32
        if($round == "true")
        {
            return round($FahrenheitVal);
        }
        else
        {
            return $FahrenheitVal;
        }
    }
}

/* Kelvin to and from conversions */

function celsiusToKelvin($cel, $round="true")
{
    global $NaN;
    if(!is_numeric($cel))
    {
        return $NaN;
    }
    else
    {
        $KelvinVal = $cel + 273;
        if($round == "true")
        {
            return round($KelvinVal);
        }
        else
        {
            return $KelvinVal;
        }
    }
}

function kelvinToCelsius($kel, $round="true")
{
    global $NaN;   
    if (!is_numeric($kel))
    {
        return $NaN;
    }
    else
    {
        $CelsiusVal = $kel - 273;   
        if ($round == "true")
        {
            return round($CelsiusVal);
        }
        else
        {
            return $CelsiusVal;
        }
    }
}

function kelvinToFahrenheit($kel, $round="true")
{
    global $NaN;   
    if(!is_numeric($kel))
    {
        return $NaN;
    }
    else
    {
        $FahrenheitVal9 * ($kel - 459) / 5
        if($round == "true")
        {
            return round($FahrenheitVal);
        }
        else
        {
            return $FahrenheitVal;
        }
    }
}

function fahrenheitToKelvin($far, $round="true")
{
    global $NaN;
    if(!is_numeric($far))
    {
        return $NaN;
    }
    else
    {
        $KelvinVal5 * ($far + 459) / 9;
        if($round == "true")
        {
            return round($KelvinVal);
        }
        else
        {
            return $KelvinVal;
        }
    }
}

function kelvinToRankine($kel, $round="true")
{
    global $NaN;
    if (!is_numeric($kel))
    {
        return $NaN;
    }
    else
    {
        $RankineVal = ($kel * 9) / 5;
        if($round == "true")
        {
            return round($RankineVal);
        }
        else
        {
            return $RankineVal;
        }
    }
}

function rankineToKelvin($ran, $round="true")
{
    global $NaN;
    if(!is_numeric($ran))
    {
        return $NaN;
    }
    else
    {
        $KelvinVal = ($ran * 5) / 9;
        if($round == "true")
        {
            return round($KelvinVal);
        }
        else
        {
            return $KelvinVal;
        }
    }
}

/* To and From Rankine using Celsius and Fahrenheit because Rankine to Kelvin is defined in Kelvin functions  */

function rankineToCelsius($ran, $round="true")
{
    global $NaN;   
    if(!is_numeric($ran))
    {
        return $NaN;
    }
    else
    {
        $CelsiusVal = 5 * ($ran - 491) / 9
        if($round == "true")
        {
            return round($CelsiusVal);
        }
        else
        {
            return $CelsiusVal;
        }
    }
}

function celsiusToRankine($cel, $round="true")
{

    global $NaN;
   
    if(!is_numeric($cel))
    {
        return $NaN;
    }
    else
    {
        $RankineVal = ($cel * 9) / 5 + 491
        if($round == "true")
        {
            return round($RankineVal);
        }
        else
        {
            return $RankineVal;
        }
    }
}

function fahrenheitToRankine($far, $round="true")
{
    global $NaN;
    if(!is_numeric($far))
    {
        return $NaN;
    }
    else
    {
        $RankineVal = $far + 459;
        if($round == "true")
        {
            return round($RankineVal);
        }
        else
        {
            return $RankineVal;
        }
    }
}

function rankineToFahrenheit($ran, $round="true")
{
    global $NaN;   
    if(!is_numeric($ran))
    {
        return $NaN;
    }
    else
    {
        $FahrenheitVal = $ran - 459;
        if($round == "true")
        {
       
            return round($FahrenheitVal);
        }
        else
        {
            return $FahrenheitVal;
        }
    }
}
?>

References


0
pankaj.sea's Avatar, Join Date: Apr 2009
Pro contributor
Good Info added...
0
shrikrishnatech's Avatar, Join Date: Nov 2010
Go4Expert Member
nice script its really helpful
0
sneha123's Avatar
Go4Expert Member
Hey ,Nice thread,,,,,this is very informative site,,,,,,thanks for this information will really help us..... thanks for sharing with us…its great post! thanks a lot for these guides..they're really helpful.,,,,,,,Well done !!!!!!!!
0
Seo_Aryan's Avatar
Banned
1.
<script type="text/javascript">
2.
var fahrenheit= Number(document.temp_conv.fahr.value);
3.
var celsius= Number(document.temp_conv.cels.value);
4.
var kelvin= Number(document.temp_conv.kelv.value);
5.

6.
if(isFinite(fahrenheit)){
7.

8.
function conversion() {
9.
var f= Number(document.temp_conv.fahr.value);
10.
var c= Number(document.temp_conv.cels.value);
11.
var k= Number(document.temp_conv.kelv.value);
12.
var f1= 5/9;
13.
var s1= f-32;
14.

15.
var cels= f1*s1
16.
document.temp_conv.cels.value = cels;
17.

18.
var kelv= cels + 273
19.
document.temp_conv.kelv.value = kelv;
20.
}
21.
}
22.

23.
if(isFinite(celsius)){
24.

25.
function conversion() {
26.
var f= Number(document.temp_conv.fahr.value);
27.
var c= Number(document.temp_conv.cels.value);
28.
var k= Number(document.temp_conv.kelv.value);
29.

30.
var fahr= 1.8*c+32
31.
document.temp_conv.fahr.value = fahr;
32.

33.
var kelv= c + 273
34.
document.temp_conv.kelv.value = kelv;
35.
}
36.
}
37.

38.
if(isFinite(kelvin)){
39.

40.
function conversion() {
41.
var f= Number(document.temp_conv.fahr.value);
42.
var c= Number(document.temp_conv.cels.value);
43.
var k= Number(document.temp_conv.kelv.value);
44.

45.
var cels= k - 273
46.
document.temp_conv.kelv.value = kelv;
47.

48.
var fahr= 1.8*cels+32
49.
document.temp_conv.fahr.value = fahr;
50.
}
51.
}
52.
</script>
53.

54.
</head>
55.
<body>
56.

57.
<form name="temp_conv">
58.
Fahrenheit<br />
59.
<input type="text" name="fahr" id="fahr" value="" /><br />
60.
Celsius<br />
61.
<input type="text" name="cels" id="cels" value="" /><br />
62.
Kelvin<br />
63.
<input type="text" name="kelv" id="kelv" value="" />
64.
<input type="button" value="Convert" onclick="conversion()" />
65.
</form>