PHP Flow Control / Repetition pt. 2
Subtitles of the Movie
A third and more elegant looping technique is the forloop. The for loop is a little bit more elegant, but takes a little bit more to master. The for loop allows you to actually declare the variable you want to use as your incrementer or your condition inside its conditional statement. So in the parentheses, I'm going to use I as my incrementer. The next thing you declare is your condition that you want to loop based upon. Well, as long as the I is less than 20. And then the third thing is your actual incrementation. So I plus plus. So here you can see that right off the bat with my parentheses I'm able to take care of several lines of code from the previous loop statements. I can declare the variable and instantiate its value. I can specify the conditions by which I want to keep looping and then specify my incrementation of my incrementer value. Then all I need to do is perform the code that I want to perform each time I loop. And I no longer need the declaration for X up above. You can always substitute the items up here in your conditional portion based upon values from your programming logic or from your FileMaker database. This is a very simple example of the for loop, but as you can see, once you master how to use it, it can become much more streamlined than the other looping functions. The last looping technique I'll show you is the foreach loop. The foreach loop is only used for going through all the elements of an array and outputting either the value of the element or the key and the value. I won't spend too much time on the foreach loop right now because we haven't really discussed arrays. But I will build a little array and just show you how it outputs it. We'll foreach each one of the elements in the array and then we'll echo out the output. So here I've created an array with for different values and then I used the foreach loop to go through all the elements in the array and output their value. There are other things that you can do with the for each looping statement that I won't get into right now but later on when we talk about arrays we'll be using foreach much more often. When you're programming using FileMaker's API for PHP, you'll be dealing with a lot of arrays and the for each loop will be one of the main functions that you use in your PHP programming. This concludes our lesson on controlling the flow of logic in your PHP application by using the different repetition or looping techniques in PHP.
Tutorial Information
| Course: | FileMaker 9 & PHP Foundations |
| Author: | Lance Hallberg |
| SKU: | 33786 |
| ISBN: | 1-933736-99-2 |
| Release Date: | 2007-08-22 |
| Duration: | 9.5 hrs / 107 lessons |
| Work Files: |
Yes |
| Captions: | For Online University members only |
| Compatibility: |
Vista/XP/2000, OS X, Linux QuickTime 7, Flash 8 |
VTC Sign up & Benefits
- Unlimited Access
- 81,350 Video Tutorials (20,800 free)
- Video Available as Flash or QuickTime
- Over 782 Courses
- $30 for One Month Access
- Multi-User Discounts Available
United States 