Convert Fahrenheit to Celsius using a Function

One way to perform the conversion is by defining a function that takes the Fahrenheit temperature as an input and returns the Celsius temperature.

Example: Illustration of Calculating Fahrenheit to Celsius in PHP using a function.

PHP




<?php
 
// Function for converting
// fahrenheit to celsius
function fahToCel($fahrenheit) {
    $celsius = ($fahrenheit - 32) * 5 / 9;
    return $celsius;
}
 
// Driver code
$fahrenheit = 98.6;
$celsius = fahToCel($fahrenheit);
 
echo "{$fahrenheit}°F is {$celsius}°C";
 
?>


Output

98.6°F is 37°C

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

Similar Reads

Convert Fahrenheit to Celsius using Inline Calculation

If you only need to perform the conversion once or in a specific context, you can do the calculation inline without defining a separate function....

Convert Fahrenheit to Celsius using a Function

...

Convert Fahrenheit to Celsius Through User Input using HTML

One way to perform the conversion is by defining a function that takes the Fahrenheit temperature as an input and returns the Celsius temperature....

Contact Us