Skip to content

Commit 99cd6bc

Browse files
committed
adding temperature conversion and functions
1 parent 098dac2 commit 99cd6bc

File tree

2 files changed

+132
-0
lines changed

2 files changed

+132
-0
lines changed

includes/temperature.php

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
require_once('units.php');
3+
require_once('floatToString.php');
4+
5+
function convertToCelsius($value, $fromUnit)
6+
{
7+
switch ($fromUnit) {
8+
case 'celsius':
9+
return $value;
10+
break;
11+
case 'fahrenheit':
12+
return ($value - 32) / 1.8;
13+
break;
14+
case 'kelvin':
15+
return $value - 273.15;
16+
break;
17+
default:
18+
return "Unsupported unit";
19+
}
20+
}
21+
22+
function convertFromCelsius($value, $toUnit)
23+
{
24+
switch ($toUnit) {
25+
case 'celsius':
26+
return $value;
27+
break;
28+
case 'fahrenheit':
29+
return ($value * 1.8) + 32;
30+
break;
31+
case 'kelvin':
32+
return $value + 273.15;
33+
break;
34+
default:
35+
return "Unsupported unit";
36+
}
37+
}
38+
39+
function convertTemperature($value, $fromUnit, $toUnit)
40+
{
41+
$celsiusValue = convertToCelsius($value, $fromUnit);
42+
$newValue = convertFromCelsius($celsiusValue, $toUnit);
43+
44+
return $newValue;
45+
}
46+
?>

temperature.php

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?php
2+
3+
require_once('includes/temperature.php');
4+
5+
// Default empty variables
6+
$fromValue = '';
7+
$fromUnit = '';
8+
$toUnit = '';
9+
$toValue = '';
10+
11+
/* Check conversion request on submit */
12+
if ($_POST['submit']) {
13+
$fromValue = $_POST['fromValue'];
14+
$fromUnit = $_POST['fromUnit'];
15+
$toUnit = $_POST['toUnit'];
16+
17+
$toValue = convertTemperature($fromValue, $fromUnit, $toUnit);
18+
}
19+
20+
$tempUnits = array(
21+
"Celsius",
22+
"Fahrenheit",
23+
"Kelvin"
24+
);
25+
?>
26+
<!DOCTYPE html>
27+
<html>
28+
<head>
29+
<meta charset="UTF-8">
30+
<title>Convert Temperature</title>
31+
<link href="styles.css" rel="stylesheet" type="text/css">
32+
</head>
33+
<body>
34+
35+
<div id="main-content">
36+
37+
<h1>Convert Temperature</h1>
38+
39+
<form action="" method="post">
40+
41+
<div class="entry">
42+
<label>From:</label>&nbsp;
43+
<input type="text" name="fromValue" value="<?php { echo $fromValue;} ?>" />&nbsp;
44+
<select name="fromUnit">
45+
<?php
46+
foreach ($tempUnits as $unit) {
47+
echo "<option value=\"" . strtolower($unit) . "\"";
48+
if ($fromUnit == strtolower($unit)) {
49+
{
50+
echo " selected";
51+
}
52+
}
53+
echo ">" . $unit . "</option>";
54+
}
55+
?>
56+
</select>
57+
</div>
58+
59+
<div class="entry">
60+
<label>To:</label>&nbsp;
61+
<input type="text" name="toValue" value="<?php { echo $toValue;} ?>" />&nbsp;
62+
<select name="toUnit">
63+
<?php
64+
foreach ($tempUnits as $unit) {
65+
echo "<option value=\"" . strtolower($unit) . "\"";
66+
if ($toUnit == strtolower($unit)) {
67+
{
68+
echo " selected";
69+
}
70+
}
71+
echo ">" . $unit . "</option>";
72+
}
73+
?>
74+
</select>
75+
76+
</div>
77+
78+
<input type="submit" name="submit" value="Submit" />
79+
</form>
80+
81+
<br/>
82+
<a href="index.php">Return to menu</a>
83+
84+
</div>
85+
</body>
86+
</html>

0 commit comments

Comments
 (0)