Course Content
Introduction to PHP
Introduction to PHP
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:
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.
main
<?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
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.
Thanks for your feedback!
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:
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.
main
<?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
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.
Thanks for your feedback!
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:
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.
main
<?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
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.
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.
main
<?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
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.