Programming Fundamentals/Functions/PHP

From Wikiversity
Jump to navigation Jump to search

subroutines.php[edit]

<?php

// This program asks the user for a Fahrenheit temperature, 
// converts the given temperature to Celsius,
// and displays the results.
//
// References:
//     https://www.mathsisfun.com/temperature-conversion.html
//     https://en.wikibooks.org/wiki/PHP_Programming

function get_f()
{
    echo "Enter Fahrenheit temperature:\n";
    $f = readline();
        
    return $f;
}

function calculate_c($f)
{
    $c = ($f - 32) * 5 / 9;
        
    return $c;
}

function display_result($f, $c)
{
    echo $f . "° Fahrenheit is " . $c . "° Celsius";
}

function main()
{
    $f = get_f();
    $c = calculate_c($f);
    display_result($f, $c);
}

main();

?>

Try It[edit]

Copy and paste the code above into one of the following free online development environments or use your own PHP compiler / interpreter / IDE.

See Also[edit]