How to Convert Celcius Degrees to Farenheit Using PHP

Kevin Amayi Feb 02, 2024
PHP
  1. Convert Celsius Degrees to Fahrenheit Using PHP Variables and Expression
  2. Convert Celsius Degrees to Fahrenheit Using a PHP Function
How to Convert Celcius Degrees to Farenheit Using PHP

We will look at how to use PHP to convert Celsius degrees to Fahrenheit using variables and expressions; you will need a file, save it as test.php and save the code.

You will also need to run this code from a server like Apache with PHP installed.

Convert Celsius Degrees to Fahrenheit Using PHP Variables and Expression

<!DOCTYPE html>
<html>
<body>
<?php
    echo "<table><tr><th>Celcius</th><th>Fahrenheit<th></tr>";
    for($celcius = 0; $celcius <= 50; $celcius+=5) {
        
        $farenheit = ($celcius * 9/5) + 32;
        
        echo "<tr><td>$celcius</td><td>$farenheit</td></tr>";
    }
    echo "</table>";
?>
</body>
</html>

Output:

    Celcius	Fahrenheit	
        0	32
        5	41
        10	50
        15	59
        20	68
        25	77
        30	86
        35	95
        40	104
        45	113
        50	122

Convert Celsius Degrees to Fahrenheit Using a PHP Function

<!DOCTYPE html>
<html>
<body>
<?php
    function calculcateFarenheit(int $celsius)
    {
        return ($celsius * 9/5) + 32;
    }

    echo "<table><tr><th>Celcius</th><th>Fahrenheit<th></tr>";
    for($celcius = 0; $celcius <= 50; $celcius+=5) {
        echo sprintf("<tr><td>%d</td><td>%d</td></tr>", $celcius, calculcateFarenheit($celcius));
    }
    echo "</table>";

?>
</body>
</html>

Output:

    Celcius	Fahrenheit	
        0	32
        5	41
        10	50
        15	59
        20	68
        25	77
        30	86
        35	95
        40	104
        45	113
        50	122