-
Found this by searching, examples of program loops, where the top result, though probably very old, is exactly what I wanted: Loop Examples.
Notice right off the top the two main categories: Count controlled, and Event controlled. The language is not PHP, but a close look will reveal the salient information on how each type of loop within these categories works, and how it is controlled.
In old BASIC speak, the simplest loop was, 10 GOTO 10. This one is the best example of an uncontrolled loop. A common use of this line was a listening post. Event listeners would trap events on the screen, the keyboard or the mouse and execute their toolbox code, but in the meantime, the program would just hang out at line 10, to which it would return after every event is handled.
More elaborate examples can be drummed up, like a non-terminating slide show, or a repeating sequence of sampled music. Uncontrolled loops are also called 'infinite' in that they have no predefined end. They stop when the program stops. We definitely don't want any of these in a our PHP scripts or the page will never load, and the hosting provider will get mighty upset if it crashes their server.
This is where controlled loops come in. We set strict limits, sometimes referred to as lower and upper bounds, when counting, or structure our data so that an end of file event occurs (such as above in the FOREACH() that PHP provides us) and forces an exit from the loop. We can also set exit conditions within a loop, which lets us minimize the clock ticks consumed by the server while executing it. We do that above, as well. Remember Z and F?
Just so we're clear, there is no one loop that is best for all purposes. With time and practice and experimentation/invention you will set upon a choice of loop method best suited to the task at hand and the type of data being crunched.
A simple counting loop (without employing actual loop functions within the given language) is, again in BASIC speak,
10 A=0
20 B=100
30 A=A+1
40 IF B-A=0 THEN END
50 GOTO 10
Here is a simple PHP counting loop
$a = 0;
$b = 100;
do { $a++; } while ( $a < $b );
echo $a . ", " . $b;
One loop function built into most languages that keep all the controls in one place is the FOR() loop. In BASIC, this used to be a FOR TO STEP NEXT loop but languages evolved that led to C which led to PHP, which give us a more elegant, FOR(). Here is one that loops through what we have above:
for ( $i = 0, $n = 100; $i < $n; $i++) { // do something }
We can already see a difference in these two loops. In the former, the increment was the action within the loop, whereas in the latter, the increment is handled by the control expressions defined by the for() arguments, leaving the 'workspace' completely free of control variables, save a possible event trap that causes an exit. The code within the workspace will execute once for every increment of the control variable, just as above and will terminate when it reaches the upper bound.
When working with one dimension arrays in PHP, the for() loop works great. It cannot equal the power of the foreach() loop when it comes to more sophisticated array structures (keyed indexes and arrays within array cells), though. While examining this language function, it will be good to immerse oneself in ASSOCIATIVE ARRAYS. They can be very confusing at first, and a clear understanding is necessary in order to properly understand and implement these loops with success. If you are familiar with JavaScript basic objects--Property : Value, then you will catch on quickly to, $key => $value.
It gets hairy when $value is an array, also. But as a single dimension, this one can be parsed using for() or foreach() again, either one nested in the main foreach(). But now I'm getting ahead of things. You will not find the code in our example above very difficult to grasp once you envision the workings of the two main loops. The only other abstracts in the code are the REGEX used in the PREG_REPACE() function, and the construction of our letter array with PREG_SPLIT(). Both of these functions require thorough reading, which you will benefit from immensely.
The string functions are pretty self explanatory, but they deserve a good reading up on, too. They are an important part of the engine, after all. Just as a fuel filter removes unwanted particles from gasoline before it reaches the injection system, our filter removes unwanted characters and forces the remainder into uppercase. The substr() function grabs the first character of the string, regardless of length. Just studying the basic PHP functions in our example enough to understand it, will open up on dozens of ways to use just these functions in powerful ways. Just remember, your PHP is not a program, it is a script, and must terminate at some point or the page will never be returned to the client.
As loops go, you'll notice that our example is an infinite loop. It keeps requesting the same page, which is both form control and handler rolled into one, just as yours is. Let me know how you're making out, and if you wish to continue this discussion about loops, bring it back here. Other PHP topics will need to be in a new thread, though, since this one is about PHP loops.
Last edited by weegillis; 04-10-2012 at 12:28 AM.
Reason: typo
-
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules