Here’s a simple Temperature Converter in PHP along with an explanation of the code. This converter allows users to convert between Celsius, Fahrenheit, and Kelvin.
Table of Contents
Project Description
The temperature converter will have:
A form where the user can enter a temperature value.
Options to select the unit of the input temperature (Celsius, Fahrenheit, or Kelvin).
Options to select the target unit for conversion (Celsius, Fahrenheit, or Kelvin).
The result of the conversion will be displayed after the form is submitted.
Complete Code
<?php // Function to convert temperature based on the input and target units function convertTemperature($value, $from_unit, $to_unit) { switch ($from_unit) { case "celsius": if ($to_unit == "fahrenheit") { return ($value * 9/5) + 32; // Celsius to Fahrenheit } elseif ($to_unit == "kelvin") { return $value + 273.15; // Celsius to Kelvin } return $value; // If target unit is the same case "fahrenheit": if ($to_unit == "celsius") { return ($value - 32) * 5/9; // Fahrenheit to Celsius } elseif ($to_unit == "kelvin") { return ($value - 32) * 5/9 + 273.15; // Fahrenheit to Kelvin } return $value; case "kelvin": if ($to_unit == "celsius") { return $value - 273.15; // Kelvin to Celsius } elseif ($to_unit == "fahrenheit") { return ($value - 273.15) * 9/5 + 32; // Kelvin to Fahrenheit } return $value; default: return "Invalid Unit"; } } // Initialize variables $result = ""; $error_message = ""; // Check if form has been submitted if (isset($_POST['submit'])) { $value = $_POST['value']; $from_unit = $_POST['from_unit']; $to_unit = $_POST['to_unit']; // Validate that the input value is numeric if (is_numeric($value)) { // Call the convertTemperature function $result = convertTemperature($value, $from_unit, $to_unit); $result = number_format($result, 2) . " " . ucfirst($to_unit); // Format result and append target unit } else { $error_message = "Please enter a valid numeric temperature value."; } } ?> <!DOCTYPE html> <html> <head> <title>Temperature Converter</title> </head> <body> <h1>Temperature Converter</h1> <!-- Display error message if any --> <?php if ($error_message): ?> <p style="color: red;"><?php echo $error_message; ?></p> <?php endif; ?> <!-- Form to input temperature value and select units --> <form method="post" action=""> <label for="value">Enter Temperature Value:</label> <input type="text" name="value" required> <br><br> <label for="from_unit">From:</label> <select name="from_unit" required> <option value="celsius">Celsius</option> <option value="fahrenheit">Fahrenheit</option> <option value="kelvin">Kelvin</option> </select> <br><br> <label for="to_unit">To:</label> <select name="to_unit" required> <option value="celsius">Celsius</option> <option value="fahrenheit">Fahrenheit</option> <option value="kelvin">Kelvin</option> </select> <br><br> <button type="submit" name="submit">Convert</button> </form> <!-- Display the result if conversion was successful --> <?php if ($result): ?> <h2>Converted Temperature: <?php echo $result; ?></h2> <?php endif; ?> </body> </html>
Explanation of the Code
1. Temperature Conversion Function
function convertTemperature($value, $from_unit, $to_unit) { switch ($from_unit) { case "celsius": if ($to_unit == "fahrenheit") { return ($value * 9/5) + 32; // Celsius to Fahrenheit } elseif ($to_unit == "kelvin") { return $value + 273.15; // Celsius to Kelvin } return $value; // If the target unit is Celsius, return the same value case "fahrenheit": if ($to_unit == "celsius") { return ($value - 32) * 5/9; // Fahrenheit to Celsius } elseif ($to_unit == "kelvin") { return ($value - 32) * 5/9 + 273.15; // Fahrenheit to Kelvin } return $value; case "kelvin": if ($to_unit == "celsius") { return $value - 273.15; // Kelvin to Celsius } elseif ($to_unit == "fahrenheit") { return ($value - 273.15) * 9/5 + 32; // Kelvin to Fahrenheit } return $value; default: return "Invalid Unit"; } }
This function convertTemperature() takes three arguments: $value (the temperature to be converted), $from_unit (the unit of the input value), and $to_unit (the target unit to convert to).
The function uses a switch statement to determine the conversion logic based on the input and target units.
It handles conversions between Celsius, Fahrenheit, and Kelvin.
If the input and target units are the same, the function simply returns the original value.
2. Processing the Form Submission
if (isset($_POST['submit'])) { $value = $_POST['value']; $from_unit = $_POST['from_unit']; $to_unit = $_POST['to_unit']; if (is_numeric($value)) { $result = convertTemperature($value, $from_unit, $to_unit); $result = number_format($result, 2) . " " . ucfirst($to_unit); // Format the result } else { $error_message = "Please enter a valid numeric temperature value."; } }
The form data is processed when the form is submitted (via $_POST[‘submit’]).
The input temperature value is retrieved from $_POST[‘value’] and validated to ensure it is numeric using is_numeric(). If it’s not numeric, an error message is displayed.
If valid, the temperature is converted by calling the convertTemperature() function, and the result is formatted to two decimal places using number_format().
The target unit is appended to the result to make it clear (e.g., “100.00 Celsius”).
3. HTML Form for Input
<form method="post" action=""> <label for="value">Enter Temperature Value:</label> <input type="text" name="value" required> <br><br> <label for="from_unit">From:</label> <select name="from_unit" required> <option value="celsius">Celsius</option> <option value="fahrenheit">Fahrenheit</option> <option value="kelvin">Kelvin</option> </select> <br><br> <label for="to_unit">To:</label> <select name="to_unit" required> <option value="celsius">Celsius</option> <option value="fahrenheit">Fahrenheit</option> <option value="kelvin">Kelvin</option> </select> <br><br> <button type="submit" name="submit">Convert</button> </form>
This form contains:
A text input field (<input type=”text” name=”value”>) for the user to enter the temperature value.
Two <select> dropdowns for selecting the “From” unit (Celsius, Fahrenheit, or Kelvin) and the “To” unit (Celsius, Fahrenheit, or Kelvin).
A submit button that triggers the conversion when clicked.
4. Displaying the Result
<?php if ($result): ?> <h2>Converted Temperature: <?php echo $result; ?></h2> <?php endif; ?>
If the form is successfully submitted and a valid conversion result is obtained, it will display the result in an <h2> element.
How the Temperature Converter Works
User Input: The user enters a temperature value, selects the input unit (e.g., Celsius), and the target unit (e.g., Fahrenheit).
Form Submission: When the form is submitted, the PHP script processes the input and validates the temperature value.
Conversion Logic: The convertTemperature() function performs the appropriate conversion based on the selected units.
Result Display: The converted temperature is displayed on the page.
Potential Improvements
Add More Units: You could add additional temperature units like Rankine.
Error Handling: You can add more robust error handling for invalid input (e.g., blank input, non-numeric input).
Styling: Use CSS to improve the look of the form and the result display.
This PHP project is a great way to practice working with forms, handling user input, and using conditional logic to perform calculations.