Loops in Php

Chapter 7 · Tamil Nadu State Board

  • 4 min read
  • 5-question quiz

Every key point below shows the sentence from your textbook it comes from. Book back answers are solved and checked against the book; answers we could not prove are left out. How we check content

In short

This lesson teaches how PHP loops repeat a block of code a set number of times or while a condition is true. It explains the for, while, do...while and foreach loops, including how each one checks or uses its control information. The key difference between while and do...while is when the condition is checked and whether the code is guaranteed to run once. These loops are useful for repeating actions and working through array elements.

Written by AI from this lesson. How we check it

Key points

  • A PHP loop repeats a block of code a certain number of times or until a condition is met.
    From your textbook
    “a loop executes a set of instructions(a block of code) repeatedly for a certain number of times or until a certain condition is met.”
  • A for loop has initialization, condition and increment or decrement parts, and checks its condition before each iteration.
    From your textbook
    “for (initialization; condition; increment/decrement) { //code to be executed; }”
  • The for loop's initialization runs once, while its increment or decrement updates variables after each iteration.
    From your textbook
    “The initialization part is executed only once at the beginning of the loop.”
  • A while loop checks its condition at the beginning, so its code may not run if the condition is false initially.
    From your textbook
    “The condition is evaluated at the beginning of each iteration of the loop.”
  • A do...while loop checks its condition at the end and runs its code block at least once.
    From your textbook
    “the code block is always executed at least once, even if the condition is false.”
  • A foreach loop goes through array elements and can use their keys and values.
    From your textbook
    “The foreach loop will iterate over each element of the array and assign the key of the element to the $key variable and the value of the element to the $value variable.”
Test yourself: 5 questions

Formulas in this chapter

  • for loop syntaxfor (initialization; condition; increment/decrement) { //code to be executed; }Explained here
  • while loop syntaxwhile (condition) //code to be executed; }Explained here
  • do...while loop syntaxdo { //code to be executed; } while (condition);Explained here
  • foreach loop syntaxforeach ($array as $value) //code to be executed;Explained here
  • foreach with keys and valuesforeach ($array as $key => $value) //code to be executed;Explained here

Loops in PHP

Definition

Loop
A loop is a control structure that runs a block of code repeatedly for a set number of times or until a condition is met.

Definition

Block of code
A block of code is a group of statements that are executed together as one unit.

Types of loops in PHP

  • for loop
  • while loop
  • do...while loop
  • foreach loop

Remember: Choose a loop based on whether you need a specific number of repetitions, a condition check, or to work through an array.

for Loop

A for loop runs a block of code a specific number of times. It is an entry-check loop. Initialization happens once at the beginning. The condition is checked before each repetition, and increment or decrement updates variables after each repetition.

for loop syntax

for (initialization; condition; increment/decrement) { //code to be executed; }

Initialization sets variables, condition decides whether the loop continues, and increment/decrement updates variables.

Worked example

Print the numbers from 1 to 5 in ascending order.

Try it first, then show the solution
  1. Start with $i = 1.
  2. Continue while $i<= 5.
  3. Print $i, then increase it with $i++.

Answer: The output is 1 2 3 4 5.

Worked example

Print the numbers from 5 to 1 in descending order.

Try it first, then show the solution
  1. Start with $i = 5.
  2. Continue while $i>= 1.
  3. Print $i, then decrease it.

Answer: The output is 5 4 3 2 1.

while and do...while Loops

A while loop checks its condition before running the code block. It is an entry-check loop, so the block may run zero times if the condition is false at the start. The condition is checked again before each repetition.

while loop syntax

while (condition) //code to be executed; }

The condition decides whether the code block runs or the loop ends.

Worked example

Print the numbers from 1 to 5 using a while loop.

Try it first, then show the solution
  1. Set $i = 1.
  2. While $i<= 5, print $i.
  3. Increase $i with $i++ after printing.

Answer: The output is 1 2 3 4 5.

A do...while loop runs the code block before checking its condition. It is an exit-check loop. The block runs at least once, even if the condition is false.

do...while loop syntax

do { //code to be executed; } while (condition);

The code block runs first, then the condition decides whether it runs again.

Worked example

Print the numbers from 1 to 5 using a do...while loop.

Try it first, then show the solution
  1. Set $i = 1.
  2. Print $i and increase it with $i++.
  3. Repeat while $i<= 5.

Answer: The output is 1 2 3 4 5.

while loop and do...while loop

while loopdo...while loop
Checks the condition before running the code block.Runs the code block before checking the condition.
May run zero times if the condition is false at the start.Runs at least once, even if the condition is false.
Entry-check loop.Exit-check loop.

foreach Loop and Arrays

A foreach loop goes through the elements of an array. For an associative array, it can also access each element's key and value. It is useful for carrying out an action on every array element.

foreach loop syntax

foreach ($array as $value) //code to be executed;

$array is the array being visited, and $value holds the current element.

Worked example

Print the elements of an array.

Try it first, then show the solution
  1. Use $array = array(1, 2, 3, 4, 5);
  2. Go through the array with foreach ($array as $value).
  3. Print each $value.

Answer: The output is 1 2 3 4 5.

foreach with keys and values

foreach ($array as $key => $value) //code to be executed;

$key holds the current element's key, and $value holds its value.

Worked example

Print the keys and values of an associative array.

Try it first, then show the solution
  1. Use $array = array(“a” => 1, “b” => 2, “c” => 3);
  2. Go through the array with foreach ($array as $key => $value).
  3. Print each key and value.

Answer: The key-value pairs are a=>1, b=>2, c=>3.

Remember: Use foreach when you want to process each element of an array.

Questions students ask

Short, direct answers to what students search for in this chapter.

What is a loop in PHP?

A loop is a control structure that runs a block of code repeatedly. It can repeat for a certain number of times or continue until a condition is met. PHP has for, while, do...while and foreach loops.

What are the three parts of a for loop in PHP?

The three parts are initialization, condition and increment/decrement. Initialization sets variables once at the beginning. The condition is checked before each iteration. Increment or decrement updates variables after each iteration.

What is the difference between while and do...while loops in PHP?

A while loop checks its condition before running the code block, so it may run zero times. A do...while loop runs the code block first and checks the condition afterward. It always runs at least once.

What is a foreach loop used for in PHP?

A foreach loop is used to go through the elements of an array. It can place each element in a value variable. With an associative array, it can also place each element's key and value in separate variables.

Book back answers

19 textbook exercises, solved and checked against the book. Questions we could not prove are left out.

Choose the correct answer

  1. Which of the following is NOT a type of loop statement in PHP?

    • a) for
    • b) if ... else
    • c) while
    • d) do … while
    Try it, then show the answer

    b) if ... else

    The chapter lists for, while, do … while and foreach as loop types. if ... else is not listed as a loop.

    From the book: “There are several types of loops in PHP. They are.”

  2. What type of loop is “for loop” in PHP?

    • a) Entry-Check Loop
    • b) Exit-Check Loop
    • c) Counter Loop
    • d) Iteration Loop
    Try it, then show the answer

    a) Entry-Check Loop

    The chapter identifies the for loop as an entry-check loop.

    From the book: “‘for loop’ is called as the entry-check loop.”

  3. What is the syntax for for loop in PHP?

    • a) for(initialization; condition; increment) { // code}
    • b) foreach(initialization; condition; decrement) { // code}
    • c) while(condition)
    • d) do{...}while(condition)
    Try it, then show the answer

    a) for(initialization; condition; increment) { // code}

    The chapter gives the for-loop syntax with initialization, condition and increment/decrement inside the parentheses, followed by a code block.

    From the book: “for (initialization; condition; increment/decrement) { //code to be executed; }”

  4. What are the three parts of the for loop syntax in PHP?

    • a) initialization, condition, increment
    • b) initialization, code block, condition
    • c) code block, condition, increment
    • d) condition, initialization, code block
    Try it, then show the answer

    a) initialization, condition, increment

    The for-loop syntax contains initialization, condition and increment/decrement as its three parts.

    From the book: “for (initialization; condition; increment/decrement)”

All 19 book back answers →

Practice

Question 1 of 5Score 0

When is the initialization part of a for loop executed?

Exam questions

Practice questions with model answers. These are not from an official paper.

What is the purpose of a loop in PHP?2 marks

A loop repeats a block of code a certain number of times or until a condition is met.

Name the three parts of a for loop and state what they do.5 marks

Initialization sets up variables and runs once at the beginning. The condition is checked before each iteration; if true, the code runs, and if false, the loop ends. Increment or decrement updates variables after each iteration.

Explain one difference between a while loop and a do...while loop.5 marks

Both while and do...while loops repeatedly execute a block of code while a condition is true. Their main difference is when the condition is checked.

  1. while loop checks first: The while loop evaluates the condition before executing the code block. If the condition is false, the loop stops without executing the block.
  2. do...while loop executes first: The do...while loop executes the code block first and evaluates the condition at the end of the iteration.
  3. Minimum number of executions: A while loop may execute zero times if its condition is false from the beginning. A do...while loop executes at least once, even if its condition is false.
  4. Loop classification: The while loop is called an entry-check loop, whereas the do...while loop is called an exit-check loop.
  5. Example from the chapter: The chapter shows a do...while loop printing numbers from 1 to 5, with the condition checked after the code block.

Example: In the chapter’s do...while example, the loop prints the numbers from 1 to 5 and then checks whether $i<= 5.

Thus, a while loop checks its condition before execution, while a do...while loop checks it afterward and runs at least once.

How does foreach work with an associative array?2 marks

It iterates over each array element and assigns the element's key to $key and its value to $value.

Watch out

  • Do not confuse the condition-check order: while checks before the code block, but do...while checks after it.
  • Do not assume every loop runs at least once; a while loop can run zero times if its condition starts false.

Still stuck on this lesson?

Get an AI answer grounded in your actual textbook — with the exact page reference.

Ask AI about this topic →