Temperature Conversion
Main Lab 5 Page
When one in required to have a section of a page repeat itself with very little deviation, actually having that section repeat is a bit redundant and pointless. Which is why loops exist.
Like any programming language, PHP allows the use of various loops, such as for
, foreach
, while
and do…while
.
The most basic of these is the while
loop. But enough of that, let's concentrate on the for
loop.
The for
loop includes setting a counter, which runs for a set number of intervals, and increments at every stage of the loop.
Below, one can see a for
loop in action. Each row after the header is built using the loop. The Celsius values are built using a counter starting at −40°, and incrementing in 10° intervals. Each time the loop runs, it also runs the conversion into Fahrenheit: F = C × 9 ÷ 5 + 32
The loop continues until the Celsius value is no longer less than or equal to 100°. So basically, it stops after 100°.
Celsius | Fahrenheit |
---|---|
-40° | -40° |
-30° | -22° |
-20° | -4° |
-10° | 14° |
0° | 32° |
10° | 50° |
20° | 68° |
30° | 86° |
40° | 104° |
50° | 122° |
60° | 140° |
70° | 158° |
80° | 176° |
90° | 194° |
100° | 212° |