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

Stryg for at vise menuen

book
For-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:

php

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.

Opgave

Swipe to start coding

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.

Løsning

Switch to desktopSkift til skrivebord for at øve i den virkelige verdenFortsæt der, hvor du er, med en af nedenstående muligheder
Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 5. Kapitel 4
single

single

Spørg AI

expand

Spørg AI

ChatGPT

Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat

close

Awesome!

Completion rate improved to 4.35

book
For-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:

php

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.

Opgave

Swipe to start coding

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.

Løsning

Switch to desktopSkift til skrivebord for at øve i den virkelige verdenFortsæt der, hvor du er, med en af nedenstående muligheder
Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

close

Awesome!

Completion rate improved to 4.35

Stryg for at vise menuen

some-alt