Loops in PHP

Go back to previous tutorial

Show all tutorials on this series till now

Hello friends,

In this article, we will study about loops in PHP. Loops are the repetition of the code. For example, if I want to print 'harish' ten times then I will have to write echo command 10 times, but using loop I will write it only once and it will repeat to print 10 times.

PHP has four types of loops:

  1. while loop
  2. do while loop
  3. for loop
  4. foreach loop

In this article, we will study about first three loops because fourth one is dependent upon array which we will learn later. So, we will learn about foreach loop later.

while loop:

In this loop, we define a initial condition and a terminating condition. The loop starts with initial condition and ends at terminating condition. The syntax of while loop is :


This condition is same as condition of if else statement. While condition is true or non-zero this code will run again and again.

Look at this example:

  1. <?php
  2. // we are giving an inital value
  3. $initial_value = 0;
  4. // 10 is terminating condition for loop
  5. while($initial_value < 10){
  6. // it's equivalent to echo "harish";echo '<br>';
  7. echo "harish".'<br>';
  8. // we are increasing $initial_value by 1
  9. $initial_value++;
  10. }
  11. ?>

In this code, $initial_value starts from 0 and the loop will run until $initial value is less than 10. $initial value is increasing 1 unit per repetition. This code will print 'harish' 10 times. 'br' is used for new line (for printing in different lines). The dot ('.') joins two printing values.

do while loop:

This loop is also same as while loop but only difference is that it checks condition later and runs code first. In other words, it checks condition only for repetition. Its syntax is:

do{.....code.......} while (condition);

For example:

  1. <?php
  2. // we are giving an inital value
  3. $initial_value = 0;
  4. // 10 is terminating condition for loop
  5. do{
  6. echo "harish".'<br>';
  7. // we are increasing $initial_value by 1
  8. $initial_value++;
  9. }while($initial_value < 10);
  10. ?>

This code is also printing 'harish ' ten times. The different occurs only when I initialized $initial_value with 10 or more value. In this case, while loop will not run but do while will. So, do while loop runs at least one time. Note the semicolon after while command in do while loop.

for loop:

The syntax of for loop is:

for( initialize, condition, operation){ ....... code ....... }

Before discussing anything first take an example:

  1. <?php
  2. // initializing inside the loop
  3. for($initial = 0; $initial <10; $initial++){
  4. echo "harish".'<br>';
  5. }
  6. // initialize outside the loop
  7. $initial_value = 0;
  8. for (; $initial_value < 10; $initial_value++) {
  9. echo "harish".'<br>';
  10. }
  11. ?>

In first two types of loops, we see that first we have to initialize a variable, then we have to check a condition for loop and at last we have to change the value of variable (increment or decrement). In for loop, we do all three things at same place. The for loop takes three arguments:

  1. initial value: This value is same as it is for first two types. In for loop, we can initialize a variable in two ways, one inside the loop (in brackets) and another before the loop. Note the syntax in above example. Each argument is separated by semicolon. If you are initializing value outside the for loop then also we have to put semicolon for empty value at initializing place.
  2. condition: condition is same as first two cases. It compares the initialized variable value and the given value.
  3. operation: operation is also same as before. It changes the value of variable after finishing code of loop each time.

More examples:

In programming just syntax is not enough to understand the language. Now try understand these examples:

  1. <?php
  2. //printing number 10 to 0
  3. for ($number=10; $number >= 0; $number--) {
  4. echo $number.'<br>';
  5. }
  6. ?>
  1. <?php
  2. //increasing number 2 every time
  3. for ($number=0; $number < 10; $number+=2) {
  4. echo $number.'<br>';
  5. }
  6. ?>

In above example, '+=' is an operator which adds right side value to left side value. Try these operators also:

'*=' (multiply like $number*=2), '-=' (subtraction), '/=' (divide), '%' (modulus or reminder) and '^=' (power).

Now we are writing a program to printing the multiple of 5 between 1 and 100. '%' returns reminder.

  1. <?php
  2. // program to print multiple of 5 between 1 and 100
  3. for ($number=1; $number < 100; $number++) {
  4. if ($number % 5 == 0) {
  5. echo $number.'<br>';
  6. }
  7. }
  8. ?>

NOTE: We are not taking any tough example here because we will take them in advance series. It is basic series for beginners.

In next tutorial we will study about strings and scopes of variables in PHP. If you like it then please share it with your friend otherwise give your valuable feedback to improve this as well as future tutorial to help other people.

Don't forget to Subscribe for latest feeds.

Comments and feedback are welcome.

Next tutorial

About Harish Kumar

Harish is an interested person in the field of web development and blogging. He works for the need of young web developers in learning various languages, latest technologies and other essential tips and tricks. If you need some help or you have some suggestion then you email him at harish@findalltogether.com without any hesitation. You can also suggest/demand for articles of your own choice.

Related Articles

In this part of free tutorial series, I am telling you about syntax, variables, data types, comments, constants and o...
Introduction to PHP
In this part of free step by step tutorial series I will teach you about for loop, while loop and do while loop of PH...
loops in PHP
In this part of free step by step tutorial series I will teach you about if, if else, nested and switch statements of...
Conditional statements in php

Login or Sign up to leave comment.