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

Scorri per mostrare il menu

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.

Compito

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.

Soluzione

Switch to desktopCambia al desktop per esercitarti nel mondo realeContinua da dove ti trovi utilizzando una delle opzioni seguenti
Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 5. Capitolo 4

Chieda ad AI

expand
ChatGPT

Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione

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.

Compito

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.

Soluzione

Switch to desktopCambia al desktop per esercitarti nel mondo realeContinua da dove ti trovi utilizzando una delle opzioni seguenti
Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 5. Capitolo 4
Switch to desktopCambia al desktop per esercitarti nel mondo realeContinua da dove ti trovi utilizzando una delle opzioni seguenti
Siamo spiacenti che qualcosa sia andato storto. Cosa è successo?
some-alt