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.

for ( $c = 0; $c <= 10; $c++ ) { echo $c; //output: 012345678910 }

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:

$animals = array('Lion','Zebra','Moose','Okapi','Panther','Penguin','Siberian Tiger','Raven'); //count() function returns the number of elements in an array for ( $i = 0; $i < count( $animals ); $i++ ) { echo $animals[ $i ].', '; } //output: Lion, Zebra, Moose, Okapi, Panther, Penguin, Siberian Tiger, Raven,

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:

//loop ends when $i equals 8 for ( $i = 0; $i < count( $animals ); $i++ ) { //are we at the end of $animals array ? Yes print dot : No print the comma echo $animals[$i] == end($animals) ? $animals[$i] : $animals[$i].', '; } //output: Lion, Zebra, Moose, Okapi, Panther, Penguin, Siberian Tiger, Raven.

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:

for ( $i = 0; ; $i++ ) { //loop ends when $i equals 8 if ($i == count( $animals )) { break; } echo $animals[$i] == end($animals) ? $animals[$i] : $animals[$i].', '; }

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:

$animals = array(0 => 'Lion', 1 => 'Zebra', 2 => 'Moose', 3 => 'Okapi', 4 => 'Panther', 5 => 'Penguin', 6 => 'Siberian Tiger', 7 => 'Raven'); echo count($animals); //output: 8

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:

for($letter = 'a'; $letter != 'z'; $letter++) { echo $letter.' '; } //output: a b c d e f g h i j k l m n o p q r s t u v w x y

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:

foreach (range('z', 'a') as $letter ) { echo $letter.' '; } //output: z y x w v u t s r q p o n m l k j i h g f e d c b a

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.

for ( $i = 0; $i < count( $animals ); $i++ ) { //print out animals }

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:

$array_count = count( $animals ); for ( $i = 0; $i < $array_count; $i++ ) { //print out animals }

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:

function everyday($start_date, $end_date) { for ($date = strtotime($start_date); $date <= strtotime($end_date); $date = strtotime("+1 day", $date)) { echo date("Y-m-d", $date)."
"; } } 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 */