DEV Community

loading...

for Loops in PHP: Converting Oven Temperatures

morinoko profile image Felice Forby ・4 min read

This is a post from my old blog, originally published in Mar. 2018

I’ve been playing around with some for loops in PHP this week. When I tried to learn about for loops before, I always had trouble with understanding all the parts like the counter initialization and iterating, but I think I’m finally getting used to it.

Here is the basic set up:

for (<initialization expression>; <test expression>; <iteration expression>) {
  //code you want to repeat
}
Enter fullscreen mode Exit fullscreen mode

So what are all those expressions?

The <initialization expression> is where you put what you need to start with.

For example, for loops are often used to go through each item in an array, so here you would set up a variable that will keep track of where you are in the array, that is, which index you are iterating on. The initial set up would be to make that variable the first index of your array, usually 0. Most of the time you’ll see something like $i = 0;.

If you’re not going through an array, it could the be the value you want to start with, as with the example below where I’m converting oven temperatures.

The <test expression> lets your loop know when to keep going and when to stop.

If you are iterating through all items in an array, you want it to stop after it does the last item. You might use something like $i < count($array); so each time the loop runs it will check whether this statement is true or false. If $i is less than the number of array elements, it will run the loop again. If not, it knows it’s finished!

If you aren’t looping through an array, use the test expression to stop after a certain number of times using a counter variable, or in the temperature conversion example below, I just want to go up to a certain temperature.

The <iteration expression> is what moves your loop along. It increases your counter variable or another testing variable after each iteration. So, if you are going through array items, it will move the array’s index forward to the next item, usually with an expression like $i++, which will increase $i by one. Or, in the temperature conversion below, I’m increasing my temperatures by five degrees at a time with $celsius += 5 until I get to the highest temperature I want, 260 degrees.

Converting Oven Temperatures

Okay, let's check out the baking temperature conversion example. So, I bake using recipes from America, Germany, and Japan. America uses Fahrenheit while Japan and Germany use Celsius. I live in Japan now, and when using American recipes, I always have to convert the Fahrenheit temps to Celsius so I can set my Japanese oven. Vice versa, when I go back to America, I still use Japanese and German recipes and constantly need to convert Celsius to Fahrenheit.

These loops below are going to give me a handy list of baking temperatures! First, C to F:

for ($celsius = 150; $celsius <= 260; $celsius += 5) {
 $fahrenheit = ($celsius * 9)/5 +32;
 printf("%d°C = %d°F\n", $celsius, $fahrenheit);
}
Enter fullscreen mode Exit fullscreen mode

I want to start with 100 degrees Celsius, so I set my initialization variable to $celsius = 150 because usually, you don’t need any temperature lower than that for baking.

I want to see the conversions up until 260 degrees Celsius, so my test expression is set to $celsius <= 260, that way the loop will keep running until 260 degrees but not beyond that.

Also, because these are oven temperatures, I just want to see temperatures in 5-degree increments. I don’t really care about what 172 degrees Celsius converts to, just 170 or 175 degrees, so I’m going to increment my $celsius variable by 5 by setting the iteration expression to $celsius += 5.

So, I’m going to convert all the values from 150 degrees Celsius to 260 degrees Celsius to Fahrenheit in five degree increments. The code inside the loop thus uses a C to F conversion equation and prints out each temperature. To avoid getting a bunch of crazy decimal point numbers, I’m also using the printf() to format the numbers in the string as whole numbers.

Let's see it in action below!

Celsius to Fahrenheit

While we’re at it, why don’t we go in the other direction too, and go from Fahrenheit to Celsius starting at 320 degrees Fahrenheit up to 500 degrees Fahrenheit. This time lets go up in 10 degree increments.

Fahrenheit to Celsius

You could use an iteration like this to print out a reference list of temperatures on your own website (e.g. print out the HTML for list items or table items).

Next Steps

I don’t know about you, but to me, these converted temperatures are a bit hard to read. 400 degrees Fahrenheit is 204 degrees Celsius. 200 degrees Celsius is 392 degrees Fahrenheit. In my next post, I’ll be exploring how we can make these numbers easier to read and use by taking advantage of PHP’s round() function!

Discussion (0)

pic
Editor guide