Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
For-Loops | Loops
Introduction to PHP
course content

Course Content

Introduction to PHP

Introduction to PHP

1. First Acquaintance
2. Variables and Data Types
3. Conditional Statements
4. Arrays
5. Loops

bookFor-Loops

A for loop repeats a particular block of code multiple times. For example, if we want to check each student's grade in a class of 32 students, we loop from 1 to 32. The for loop is used to repeat a section of code a known number of times.

Some everyday examples of using a for loop:

  • Calculating the total cost of items in a shopping cart. For instance, summing up the prices of all items in a list of purchases;
  • Printing all even days in a month. For example, printing all the even days in July;
  • Iterating through a guest list for a party. For example, printing the names of all guests.

Syntax


Let's look at the syntax of the for loop using the example code below:

The for loop has three parts:

  • Initialization is the process of setting the initial value of the variable i to 0.
  • Condition is the condition that determines whether the loop will continue iterating, checking if i is less than 5.
  • Increment or Decrement are the operations performed on the counter at the end of each loop iteration.
php

main

copy
12345
<?php for ($i = 1; $i <= 5; $i++) { echo "Iteration {$i}\n"; } ?>
  • for loop in PHP is used to iterate a specific number of times.

  • $i = 1; - Initializes the variable $i with a value of 1 before starting the loop.

  • $i <= 5; - Condition that is checked before each iteration. The loop continues as long as this condition is true.

  • $i++ - Increment operation that increases the value of $i by 1 after each iteration.

This code will print "Iteration 1" through "Iteration 5" because the condition $i <= 5 is true for values of $i from 1 to 5.

Task
test

Swipe to show code editor

Fill in the blanks in the given code so that the message "Programming is fun!" is displayed three times. Use the variable $i as the counter for the for loop.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 5. Chapter 4
toggle bottom row

bookFor-Loops

A for loop repeats a particular block of code multiple times. For example, if we want to check each student's grade in a class of 32 students, we loop from 1 to 32. The for loop is used to repeat a section of code a known number of times.

Some everyday examples of using a for loop:

  • Calculating the total cost of items in a shopping cart. For instance, summing up the prices of all items in a list of purchases;
  • Printing all even days in a month. For example, printing all the even days in July;
  • Iterating through a guest list for a party. For example, printing the names of all guests.

Syntax


Let's look at the syntax of the for loop using the example code below:

The for loop has three parts:

  • Initialization is the process of setting the initial value of the variable i to 0.
  • Condition is the condition that determines whether the loop will continue iterating, checking if i is less than 5.
  • Increment or Decrement are the operations performed on the counter at the end of each loop iteration.
php

main

copy
12345
<?php for ($i = 1; $i <= 5; $i++) { echo "Iteration {$i}\n"; } ?>
  • for loop in PHP is used to iterate a specific number of times.

  • $i = 1; - Initializes the variable $i with a value of 1 before starting the loop.

  • $i <= 5; - Condition that is checked before each iteration. The loop continues as long as this condition is true.

  • $i++ - Increment operation that increases the value of $i by 1 after each iteration.

This code will print "Iteration 1" through "Iteration 5" because the condition $i <= 5 is true for values of $i from 1 to 5.

Task
test

Swipe to show code editor

Fill in the blanks in the given code so that the message "Programming is fun!" is displayed three times. Use the variable $i as the counter for the for loop.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 5. Chapter 4
toggle bottom row

bookFor-Loops

A for loop repeats a particular block of code multiple times. For example, if we want to check each student's grade in a class of 32 students, we loop from 1 to 32. The for loop is used to repeat a section of code a known number of times.

Some everyday examples of using a for loop:

  • Calculating the total cost of items in a shopping cart. For instance, summing up the prices of all items in a list of purchases;
  • Printing all even days in a month. For example, printing all the even days in July;
  • Iterating through a guest list for a party. For example, printing the names of all guests.

Syntax


Let's look at the syntax of the for loop using the example code below:

The for loop has three parts:

  • Initialization is the process of setting the initial value of the variable i to 0.
  • Condition is the condition that determines whether the loop will continue iterating, checking if i is less than 5.
  • Increment or Decrement are the operations performed on the counter at the end of each loop iteration.
php

main

copy
12345
<?php for ($i = 1; $i <= 5; $i++) { echo "Iteration {$i}\n"; } ?>
  • for loop in PHP is used to iterate a specific number of times.

  • $i = 1; - Initializes the variable $i with a value of 1 before starting the loop.

  • $i <= 5; - Condition that is checked before each iteration. The loop continues as long as this condition is true.

  • $i++ - Increment operation that increases the value of $i by 1 after each iteration.

This code will print "Iteration 1" through "Iteration 5" because the condition $i <= 5 is true for values of $i from 1 to 5.

Task
test

Swipe to show code editor

Fill in the blanks in the given code so that the message "Programming is fun!" is displayed three times. Use the variable $i as the counter for the for loop.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

A for loop repeats a particular block of code multiple times. For example, if we want to check each student's grade in a class of 32 students, we loop from 1 to 32. The for loop is used to repeat a section of code a known number of times.

Some everyday examples of using a for loop:

  • Calculating the total cost of items in a shopping cart. For instance, summing up the prices of all items in a list of purchases;
  • Printing all even days in a month. For example, printing all the even days in July;
  • Iterating through a guest list for a party. For example, printing the names of all guests.

Syntax


Let's look at the syntax of the for loop using the example code below:

The for loop has three parts:

  • Initialization is the process of setting the initial value of the variable i to 0.
  • Condition is the condition that determines whether the loop will continue iterating, checking if i is less than 5.
  • Increment or Decrement are the operations performed on the counter at the end of each loop iteration.
php

main

copy
12345
<?php for ($i = 1; $i <= 5; $i++) { echo "Iteration {$i}\n"; } ?>
  • for loop in PHP is used to iterate a specific number of times.

  • $i = 1; - Initializes the variable $i with a value of 1 before starting the loop.

  • $i <= 5; - Condition that is checked before each iteration. The loop continues as long as this condition is true.

  • $i++ - Increment operation that increases the value of $i by 1 after each iteration.

This code will print "Iteration 1" through "Iteration 5" because the condition $i <= 5 is true for values of $i from 1 to 5.

Task
test

Swipe to show code editor

Fill in the blanks in the given code so that the message "Programming is fun!" is displayed three times. Use the variable $i as the counter for the for loop.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Section 5. Chapter 4
Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
We're sorry to hear that something went wrong. What happened?
some-alt