Lesson 14 – PHP for loop Example

Hello viewer, You can now have our Tutorial Lessons in your android mobile device and read it offline.
Download kotlin Programming APP on PlayStore
Download Website SEO Lessons APP on PlayStore

Lesson 14 - PHP for loop Example - Lesson 14 - PHP for loop Example - Lesson 14 - PHP for loop Example - PHP for loop
PHP for loop

Similar to the while loop, the for loop can be used to execute statements as long as a particular condition is met. The syntax of the for loop is as follows:

<?Php
for(start value, condition, looping) {
 //  instructions
}
?>

An example would be:

<?php
for($i=0; $i < 10; $i++) {
   echo "$i, ";
}
?>

In the above example, a start value is first defined, in which case the variable $i is initialized to the value 0. The condition of the loop is $10, ie the instructions of the loop are executed as long as this condition is fulfilled. The looping step is $i++ in the above case . This step is done after every loop of PHP, ie in the above case the value of the variable $i is incremented by 1 after the loop iteration.

The start value, the condition and also the loop step are optional. That means you can define them, but you do not have to. A completely empty for loop works as well, even if it does not necessarily make much sense:

<?Php
$i = 0;
for ( ; ; ) {
//No start value, condition or loop step

  if ($i >= 20) { 
//Stop looping 

if( $i >= 20)
     break;
  }
  echo "$i, ";
  $i++;
}
?>

Loop processes influence by means of break and continue

As with the while loop, the loop can be looped by the break commands on the for loop and continue influence. By means of break. In the process, the further loop through is interrupted and continued by means of  continue  a loop pass is skipped:

<?Php
for ($i = 0; $i < 20; $i++) {
   if ($i == 13) {
     echo "Thirteen is an unlucky number !!! <br>";
     break;
   }
   echo "$i,";
}
?>

In the above example, the loop pass is aborted as soon as the number 13 is reached. If you change the looping step from $i++   to eg $i+=2, then $i will never get the number 13 and the even numbers will be returned up to 20.

Unlike break, continue only skips a single loop step:

<?Php
for($i = 0; $i <20; $i++) {
   if ($i == 13) {
     echo "Thirteen is an unlucky number !!! <br>";
     continue;
   }
   echo "$i,";
}
?>

Difference to the while loop

The difference to the previously presented while loop lies only in the spelling. Both types of loop allow the same functionality to be implemented.

But because of the simpler syntax, the for loop is mostly used when counting up values. In a while loop, the start value, the condition, and the final loop step to increment the count variable are spread over three lines; for a for loop, all information is in one and the same line.

On the other hand, the while loop is used more frequently if it is not clear in advance how many loop digests are needed. For example, you may want to search a text file until a particular word is found. Or you go through a table in a database and want to output all lines.

//coding180.com/php/how-to-use-do-while-loop/

About The Author

Related posts

Leave a Reply