Jump to content

Applied Programming/Variables/PHP

From Wikiversity

variables.php

[edit | edit source]
<?php

// This program converts a Fahrenheit temperature to Celsius.
//
// Input:
//     Fahrenheit temperature
//
// Output:
//     Fahrenheit temperature
//     Celsius temperature
//
// Example:
//     Enter Fahrenheit temperature:
//     122
//     122 Fahrenheit is 50 Celsius
//    
// References:
//     * http://www.mathsisfun.com/temperature-conversion.html
//     * http://php.net/manual/en/features.commandline.io-streams.php
//     * https://stackoverflow.com/questions/481466/php-string-to-float

$temperature_difference = 32;
$temperature_ratio = 5 / 9;

echo ("Enter Fahrenheit temperature:");
$fahrenheit = (float) rtrim(fgets(STDIN));

$celsius = ($fahrenheit - $temperature_difference) * $temperature_ratio;

echo $fahrenheit . " Fahrenheit is " . $celsius . " Celsius!";

?>

Try It

[edit | edit source]

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 | edit source]