Convert Fahrenheit to Celsius Through User Input using HTML
In a more interactive scenario, you might want to convert a temperature entered by the user. This can be done using an HTML form to get the Fahrenheit temperature as input and PHP to process this input.
Example: Illustration of Calculating Fahrenheit to Celsius in PHP through User Input using HTML.
PHP
<!DOCTYPE html> <html> <head> <title>Fahrenheit to Celsius Conversion</title> </head> <body> <form action= "" method= "post" > <label for = "fahrenheit" >Fahrenheit:</label> <input type= "text" id= "fahrenheit" name= "fahrenheit" > <input type= "submit" value= "Convert" > </form> <?php if (isset( $_POST [ 'fahrenheit' ])) { $fahrenheit = $_POST [ 'fahrenheit' ]; $celsius = ( $fahrenheit - 32) * 5 / 9; echo "<p>{$fahrenheit}°F is {$celsius}°C</p>" ; } ?> </body> </html> |
Output:
Time Complexity: O(1)
Auxiliary Space: O(1)
PHP Program to Convert Fahrenheit to Celsius
Converting temperature from Fahrenheit to Celsius is a common task in many applications. The formula to convert Fahrenheit to Celsius is:-
Celsius = (Fahrenheit - 32) * 5/9
In this article, we will explore how to implement this conversion in PHP using different approaches such as inline calculations, functions, and using HTML.
Table of Content
- Using Inline Calculation
- Using a Function
- Through User Input using HTML