Do While Looping6:23 with Alena Holligan
In programming, a loop is a way to repeat the same actions a certain number of times, or until a certain condition is true. Instead of typing the exact same code, a loop will allow us to write that code one time then repeat that code WHILE a condition is true.
while loops are the simplest type of loop in PHP. While loops tell PHP to execute the nested statement(s) repeatedly, as long as the while expression evaluates to TRUE. The value of the expression is checked each time at the beginning of the loop, so even if this value changes during the execution of the nested statement(s), execution will not stop until the end of the iteration (each time PHP runs the statements in the loop is one iteration). Sometimes, if the while expression evaluates to FALSE from the very beginning, the nested statement(s) won't even be run once.
do-while loops are very similar to while loops, except the truth expression is checked at the end of each iteration instead of in the beginning. The main difference from regular while loops is that the first iteration of a do-while loop is guaranteed to run (the truth expression is only checked at the end of the iteration), whereas it may not necessarily run with a regular while loop (the truth expression is checked at the beginning of each iteration, if it evaluates to FALSE right from the beginning, the loop execution would end immediately).
[MUSIC] 0:00 In programing a loop is a way to repeat the same action a certain number of 0:04 times or until a certain condition is met. 0:09 For example, say you wanted to display ten random numbers on a web page. 0:13 You could write the code to generate one random number, then print it to the page, 0:18 then write that exact same code a second time, a third time, 0:23 a fourth time until you've duplicated that same code ten times. 0:27 Typing the exact same code ten times seems like a lot of work. 0:32 Now imagine you wanted 100 random numbers or 1,000. 0:36 That would be a whole lot of code. 0:41 Here's a better way. 0:43 Write that code one time then repeat it ten times or 100 or 1000. 0:45 That's exactly what a loop does. 0:51 Think of a loop as a race track. 0:54 Say you were organizing a 10,000 meter race. 0:56 You could build one really long path for 0:58 the racers to run on but that would take up a lot of space. 1:02 It would take a long time to build and it would be very expensive. 1:06 Or you could create a much shorter path 1:10 that racers run around over and over again. 1:13 The oval shape of a track is a loop and 1:16 a race requires the racers to run around the track a certain number of times. 1:19 When a racer has gone around the track ten times for 1:24 example then that racer has completed the race. 1:27 Programming loops work like this also. 1:31 The code that you want to run more than once is inside the loop. 1:34 It's like the oval part of the track. 1:39 When you enter the loop, the code inside it runs once, then it runs again and 1:41 again until that particular condition is met. 1:46 For example when the loop has run ten times then the program exits the loop or 1:49 exits the track. 1:55 PHP provides several different ways to create loops. 1:57 You'll learn about them in this lesson. 2:01 But let's start with what's called a while loop. 2:02 Remember conditional statements? 2:06 A conditional statement runs a block of code if a particular condition is true. 2:07 A basic conditional statement looks like this and 2:13 inside the parentheses goes a condition. 2:17 If the condition is true then the code inside the block runs. 2:20 In this example if the value inside the variable name 2:24 is the string Treehouse a message displays. 2:28 A while loop looks very much the same. 2:32 There's the while keyword followed by a set of parentheses, 2:35 a condition also goes inside that set of parentheses. 2:39 But instead of the code inside the block running only once when the condition is 2:43 true it runs over and over and over again while the condition remains true. 2:48 Let's see how to tackle the challenge of writing the last 100 years 2:54 to a page using a loop. 2:58 Let's start with the new file named loops.php. 3:01 After opening and closing the PHP tags, 3:08 we're going to set a variable named $currentYear. 3:12 I could hard code that as 2016, but no need to make more work for 3:19 myself to update this next year. 3:23 I'll use the date function instead. 3:26 Date, and then the uppercase Y for year. 3:29 Now I'm going to add a new variable for the year I'm going to start with. 3:33 $year = $currentYear- 100;. 3:37 Now I'm ready to create my while loop. 3:44 I want to loop while ($year <= $currentYear). 3:49 On each loop, I want to display the year, echo $year. 4:00 We'll add a line break to this. 4:06 We then need to increment the year. 4:15 It's important that you actually increment the year or your loop will run 4:19 forever or until your server runs out of memory which is no good. 4:24 Let's preview our script in the browser because it will be 4:29 easier to see all 100 lines. 4:32 Great we see 101 years 4:34 with the current year being the final year and we did this in six lines of code. 4:39 Let's jump back to the code and shorten this further. 4:45 We'll combine our incrementation with the while loop. 4:48 I'm going to put my incrementing before the year 4:52 because I want to compare the incremented value. 4:57 Let's remove the incrementing from down here. 5:01 Now let's refresh the browser. 5:04 We see 100 years now because I'm incrementing the year 5:07 before I'm displaying it. 5:11 So the first value is 1917. 5:12 I actually want to start at 1917 because I want it to show 100 years. 5:14 Another way to use a while loop is the do while loop. 5:21 If you want something to happen at least once before checking a condition, 5:25 you can start with the block of code you want to run. 5:30 Let's comment this out. 5:34 And add our do. 5:37 We start with the keyword do and then within curly braces, 5:39 we add our statement echo, $year and a break. 5:43 Then you add the while to the end of the loop, while (+ 5:51 + $year < = $currentYear). 5:56 And with a semi-colon. 6:02 Let's refresh the browser again. 6:04 This time we see that it starts with 1916, 6:07 because we're displaying the year before we increment for the first time. 6:09 If I wanted to start with 1917, 6:16 I could change my year to be 99 less than the current year. 6:17
You need to sign up for Treehouse in order to download course files.Sign up