PHP For Loop
PHP for loop is the most common type of loop used in PHP which keeps executing code until a conditional expression evaluates to false. The loop usually consists of three expressions: 1) define the counter 2) conditional expression (evaluate the counter) 3) modify the counter - which are separate by a semi-colon.
You may be wondering why, $c is not reset to zero, every time this loop runs. That's because that first expression is only run once in a for loop, during the first iteration. The second and third expressions, however, are run continuously during every iteration.
Looping Through Numeric Arrays
For loop is very useful, if you want to loop through a numeric array:
Remember that numeric array keys are automatically created when we don't specify them, if you'd like to learn more about arrays, check out PHP Array page.
We used count() function to get the number of elements in an array, so that we know when to stop our loop.
You may have noticed, there is an unwanted comma at the end of our animal list after Raven. We can use another array function called end() to check if we are at the last array value and combine that with conditional expression to remove the comma from the last animal name:
Using Breaks to End For Loop
The syntax of the for loop is not as strict as it seems. For example, we can choose to leave out the conditional expression and use break; inside the loop instead to let PHP know when to end our loop. The following would be equivalent to previous example:
There is one thing that I think might be confusing about looping through numeric arrays from the examples above, if you're new to this. The numeric array index keys start their count from 0, but the count() function starts its count from 1. So in our $animals array, we have keys that starts with 0, and the last key is 7. Here is what it would look like if we wrote numeric keys manually:
So the count() function returns 8 because it starts it's count from 1 but we have last array key which is 7. That's why we want to end our loop when $i reaches 8, since there is no key 8 in our array.
Looping Through Letters
We can use for loop to loop through letters as well:
When looping through letters in this manner, the <= will not produce the desired result, so make sure to use != (not equal) operator instead. Also, I haven't found a way to loop through the alphabet backwards using the for loop, but we can use a foreach loop and range() function to accomplish that easily:
Optimizing For Loop
Let's re-examine our earlier example where we looped through $animals array and used count() function to know when to end our loop.
How can we optimize this loop to make it faster and more efficient? Well, we already know that second and third expressions are executed during every loop iteration. The second expression which is count( $animals); uses count() function which is called repeatedly during every iteration. We can avoid having to run the count() function repeteadly within the loop, by placing it outside the loop and storing our count value in a variable:
Doing so will make our code more efficient because count() function is only run once instead of 8 times.
For Loop Practical Example
Here is a function that takes two parameters, $start_date and $end_date and loops through every day using a for loop:
"; } } everyday("2017-01-01","2017-01-20"); /* output: 2017-01-01 2017-01-02 2017-01-03 2017-01-04 2017-01-05 2017-01-06 2017-01-07 2017-01-08 2017-01-09 2017-01-10 */