In this tutorial we're going to look at a looping construct in PHP called the While Loop. Let's go ahead and open up the 0303.php file and what we're going to do is we're going to loop through a certain code and evaluate criteria to see if we want to exit the loop or if we want to process some sort of logic. We'll do this by typing in While and then in a parenthesis we'll put our condition. So let's say while the variable X is less than 20 and then in open and closing curly braces we'll put the code to process. We'll say we want to echo X to the browser and then follow it with a break tag. So while X is less than 20 then echo X and output it to the browser. I'm going to leave that right there and go ahead and refresh 0303.php page. And notice that we have an undefined variable but even more importantly notice that our script continues to run. You can see the domino wheel up here at the right. And this is because it's evaluating X and then looping and X doesn't change. So it's a continuous loop. What will happen here in PHP is that there's a timeout in your PHP.INI file based on the configuration, it'll keep going until the server actually just drops it, way longer than what you want your users to be sitting here. Most likely they'll go ahead and quit their browser or close the page before that happens. So watch out for the continuous loop. It will tie the server up or process in the server for an indefinite period of time based upon your PHP file. It could be a minute, a minute and a half and feel free to open up your configuration file and change that if you need to. We'll close our browser and then we'll go ahead and say that we want to change the value of X to be X is equal to X plus one. And now let's open up our browser and evaluate that again. So 0303.php and here's our output to the browser. Now there's a shortcut when you want to increment a variable. If you want to increment it just once you can actually just say the variable plus, plus. And that actually increases it by one. If you want to decrement it by one you can use minus, minus. So we'll output that and then I'm going to go ahead and just declare X to be equal to zero so we don't have our error that's shows up, that it's undefined variable. And we'll review that again and there we have zero through 19. Now there's a variation of the While Loop it's called the Do While. So what's happening here is that it's while X is less than 20 it's going to evaluate this code. But if X started out to be 21 or 20 even it would immediately be false and nothing would be executed. And sometimes in your code you'll want at least one execution and to do that you would use the Do While Loop. And how you do that is you just move your whole While Statement down to the bottom and then at the top you, before the opening curly bracket you use the word Do. So do whatever code is inside the curly braces while these condition are met. But it doesn't get to this evaluation until it does something at least once. So if we put this back the way it was save it and then we refresh our browser, we get no output. But if we go ahead and move forward to our new Do While Loop and then we save it and refresh the browser we get at least one output. So that's the differences between the While and the Do While Looping construct. Very helpful to use this in PHP.
| Course: | FileMaker and PHP Foundations |
| Author: | Lance Hallberg |
| SKU: | 34393 |
| ISBN: | 978-1-61866-077-0 |
| Release Date: | 2012-12-07 |
| Duration: | 7.5 hrs / 97 lessons |
| Work Files: |
Yes |
| Captions: | No |
| Compatibility: |
Vista/XP/2000, OS X, Linux QuickTime 7, Flash 8 |