Username:
Password:
FileMaker 9 & PHP Foundations Tutorials

PHP Flow Control / Repetition pt. 1

Subtitles of the Movie

Part of learning to control the flow of logic in PHP is understanding its different loop functions. In FileMaker, we have one loop function. But in PHP, there are many different loop functions and each one provides its own set of advantages and disadvantages. I'm going to open up the 03.php file and begin with the while loop. The while loop allows you to specify, in parentheses, the condition that you want to keep looping on and then, in open and closing curly brackets, execute the PHP code while you're continuing through the loops. For instance, I'm going to echo out the HTML break tag and then the value of the variable X each time we loop. In my while statement, I'm going to say while X is less than 20. One thing to note here, if I don't increment X, X will never reach 20 and we will have just created an infinite loop. If you create an infinite loop in PHP and you go to refresh that page or hit that page in your browser, your browser's going to hang and the web server's going to spawn a process for the Apache server that also hangs with the browser. Now, Apache will timeout eventually, but you definitely don't want to create infinite loops that aren't controlled. In this case, after we're done echoing, we'll increment X as such. Then X will eventually get up to 20 and we'll break our while loop. Let's view this in the browser. Here we received all the numbers one through 19. Then, of course, we got our notice of an undefined variable because we began to use X before we actually assigned it a value. As we discussed in earlier lessons, you can use undefined variables, but you must set your display errors to off. That's the while loop. Another looping function in PHP, which is very close to the while loop, is the do while. And the difference is we will actually tell PHP what to do and then at the end specify the while condition. This forces PHP to execute at least one line of the loop before we check to see if the condition is met, whereas in the while loop, we check our condition first so if the condition is not met right off the bat, none of the lines will be executed. There are times in web programming when you want to output at least one line of your loop and that is where the do while has its advantage. Here I'll say while X is less than zero. Now, we know that X is not less than zero, but if we refresh the page we're going to get at least one line of output. So that's the difference between the while and do while loops.

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