Temperature conversions between Celsius and Fahrenheit using PHP.

Here’s an example of a PHP 8.2 class that handles temperature conversions between Celsius (C) and Fahrenheit (F):

<?php

class TemperatureConverter
{
    /**
     * Convert Celsius to Fahrenheit.
     *
     * @param float $celsius The temperature in Celsius.
     * @return float The temperature in Fahrenheit.
     */
    public static function celsiusToFahrenheit(float $celsius): float
    {
        return ($celsius * 9 / 5) + 32;
    }

    /**
     * Convert Fahrenheit to Celsius.
     *
     * @param float $fahrenheit The temperature in Fahrenheit.
     * @return float The temperature in Celsius.
     */
    public static function fahrenheitToCelsius(float $fahrenheit): float
    {
        return ($fahrenheit - 32) * 5 / 9;
    }
}

// Example usage
$celsius = 25;
$fahrenheit = 77;

echo "$celsius °C is " . TemperatureConverter::celsiusToFahrenheit($celsius) . " °F" . PHP_EOL;
echo "$fahrenheit °F is " . TemperatureConverter::fahrenheitToCelsius($fahrenheit) . " °C" . PHP_EOL;

?>

This class provides two static methods:

  1. celsiusToFahrenheit: Converts a temperature from Celsius to Fahrenheit.
  2. fahrenheitToCelsius: Converts a temperature from Fahrenheit to Celsius.

The example usage demonstrates how to use the class to perform conversions. Let me know if you’d like to expand this functionality!

Loading

Leave a Reply

Your email address will not be published. Required fields are marked *