SakhaliaNetHome PageMilitary HistoryVorKutaAcceptance of cookiesAcceptance of cookies

PHP Tutorial :: Exercises (II)

PHP Exercise #5

Table of temperatures with while()

Use a while() loop for printing a table of equivalences of temperatures in Fahrenheit and Celsius degrees, from -50º F to 50º F divided in stretches of 5 degrees. In the Fahrenheit scale, water freezes at 32º and boils at 212º, while in the Celsius scale, those values are 0º and 100º respectively. Hence, for converting from Fahrenheit to Celsius, you must rest 32 to temperature, multiply by 5 and divide by 9, while for converting from Celsius to Fahrenheit, you must multiply by 9, divide by 5 and add 32.

<?php
$fahr = -50;
$stop_fahr = 50;
print '<table summary="Temperatures">';
print '<tr><th>Fahrenheit</th><th>Celsius</th></tr>';
while ($fahr <= $stop_fahr) {
$celsius = ($fahr - 32) * 5 / 9;
printf ("<tr><td>$fahr</td><td>%.2f</td></tr>", $celsius);
$fahr += 5;
}
print '</table>';
?>
FahrenheitCelsius
-50-45.56
-45-42.78
-40-40.00
-35-37.22
-30-34.44
-25-31.67
-20-28.89
-15-26.11
-10-23.33
-5-20.56
0-17.78
5-15.00
10-12.22
15-9.44
20-6.67
25-3.89
30-1.11
351.67
404.44
457.22
5010.00

PHP Exercise #6

Table of temperatures with for()

Use a for() loop for printing a table of equivalences of temperatures in Fahrenheit and Celsius degrees, from -50º F to 50º F divided in stretches of 5 degrees. In the Fahrenheit scale, water freezes at 32º and boils at 212º, while in the Celsius scale, those values are 0º and 100º respectively. Hence, for converting from Fahrenheit to Celsius, you must rest 32 to temperature, multiply by 5 and divide by 9, while for converting from Celsius to Fahrenheit, you must multiply by 9, divide by 5 and add 32.

<?php
$fahr = -50;
$stop_fahr = 50;
print '<table summary="Temperatures">';
print '<tr><th>Fahrenheit</th><th>Celsius</th></tr>';
for ($fahr = -50; $fahr <= 50; $fahr += 5) {
$celsius = ($fahr - 32) * 5 / 9;
printf ("<tr><td>$fahr</td><td>%.2f</td></tr>", $celsius);
}
print '</table>';
?>
FahrenheitCelsius
-50-45.56
-45-42.78
-40-40.00
-35-37.22
-30-34.44
-25-31.67
-20-28.89
-15-26.11
-10-23.33
-5-20.56
0-17.78
5-15.00
10-12.22
15-9.44
20-6.67
25-3.89
30-1.11
351.67
404.44
457.22
5010.00