Bird
Raised Fist0
PHPprogramming~10 mins

Why loops are needed in PHP - Test Your Understanding

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to print numbers from 1 to 5 using a loop.

PHP
<?php
for ($i = 1; $i [1] 5; $i++) {
    echo $i . "\n";
}
?>
Drag options to blanks, or click blank then click option'
A<
B>
C>=
D<=
Attempts:
3 left
💡 Hint
Common Mistakes
Using < will only print 1 to 4.
Using > or >= will not run the loop correctly.
2fill in blank
medium

Complete the code to sum numbers from 1 to 10 using a loop.

PHP
<?php
$sum = 0;
for ($num = 1; $num [1] 10; $num++) {
    $sum += $num;
}
echo $sum;
?>
Drag options to blanks, or click blank then click option'
A<=
B<
C>
D>=
Attempts:
3 left
💡 Hint
Common Mistakes
Using < will exclude 10 from the sum.
Using > or >= will not run the loop correctly.
3fill in blank
hard

Fix the error in the loop condition to print even numbers from 2 to 10.

PHP
<?php
for ($i = 2; $i [1] 10; $i += 2) {
    echo $i . "\n";
}
?>
Drag options to blanks, or click blank then click option'
A>
B<
C<=
D>=
Attempts:
3 left
💡 Hint
Common Mistakes
Using < excludes 10 from the output.
Using > or >= will cause the loop not to run.
4fill in blank
hard

Fill both blanks to create an associative array of squares for numbers 1 to 5.

PHP
<?php
$squares = [];
for ($i = 1; $i <= 5; $i++) {
    $squares[[1]] = [2];
}
print_r($squares);
?>
Drag options to blanks, or click blank then click option'
A$i
B$i * $i
C$i + 1
D$i - 1
Attempts:
3 left
💡 Hint
Common Mistakes
Using $i + 1 or $i - 1 will give wrong keys or values.
Swapping keys and values causes incorrect array.
5fill in blank
hard

Fill all three blanks to create a filtered array of numbers greater than 3 with their squares.

PHP
<?php
$numbers = [1, 2, 3, 4, 5];
$result = [];
foreach ($numbers as [1]) {
    if ([1] > 3) {
        $result[[2]] = [3];
    }
}
print_r($result);
?>
Drag options to blanks, or click blank then click option'
A$num
B$num * $num
D$number
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names causes errors.
Not filtering numbers greater than 3.

Practice

(1/5)
1. Why do we use loops in PHP programming?
easy
A. To stop the program from running
B. To store data in variables
C. To create new functions
D. To repeat a block of code multiple times without rewriting it

Solution

  1. Step 1: Understand the purpose of loops

    Loops allow repeating the same code many times without writing it again and again.
  2. Step 2: Compare other options

    Storing data, creating functions, or stopping programs are not the main reasons for loops.
  3. Final Answer:

    To repeat a block of code multiple times without rewriting it -> Option D
  4. Quick Check:

    Loops repeat code = D [OK]
Hint: Loops repeat code to avoid rewriting [OK]
Common Mistakes:
  • Thinking loops store data
  • Confusing loops with functions
  • Believing loops stop the program
2. Which of the following is the correct syntax to start a for loop in PHP?
easy
A. for ($i = 0; $i < 5; $i++) {
B. for i = 0 to 5 {
C. loop ($i = 0; $i < 5; $i++) {
D. foreach ($i = 0; $i < 5; $i++) {

Solution

  1. Step 1: Identify correct PHP for loop syntax

    PHP uses for ($i = 0; $i < 5; $i++) { to start a for loop.
  2. Step 2: Check other options for syntax errors

    Options B, C, and D use incorrect keywords or formats not valid in PHP.
  3. Final Answer:

    for ($i = 0; $i < 5; $i++) { -> Option A
  4. Quick Check:

    PHP for loop syntax = A [OK]
Hint: PHP for loops use semicolons and parentheses [OK]
Common Mistakes:
  • Using 'loop' instead of 'for'
  • Missing semicolons in for loop
  • Using 'foreach' incorrectly
3. What will be the output of this PHP code?
$sum = 0;
for ($i = 1; $i <= 3; $i++) {
    $sum += $i;
}
echo $sum;
medium
A. 3
B. 123
C. 6
D. 0

Solution

  1. Step 1: Understand the loop iterations

    The loop runs with $i = 1, 2, 3.
  2. Step 2: Calculate the sum during each iteration

    sum = 0+1=1, then 1+2=3, then 3+3=6.
  3. Final Answer:

    6 -> Option C
  4. Quick Check:

    1+2+3 = 6 [OK]
Hint: Add numbers inside loop to get sum [OK]
Common Mistakes:
  • Concatenating numbers instead of adding
  • Off-by-one errors in loop condition
  • Ignoring initial sum value
4. Find the error in this PHP loop code:
for ($i = 0; $i < 5; $i++)
    echo $i
medium
A. Missing semicolon after echo statement
B. Incorrect loop variable name
C. Loop condition should be <= 5
D. for loop cannot be used without braces

Solution

  1. Step 1: Check syntax inside the loop

    The echo statement is missing a semicolon at the end.
  2. Step 2: Verify other parts of the loop

    Loop variable and condition are correct; braces are optional for single statements.
  3. Final Answer:

    Missing semicolon after echo statement -> Option A
  4. Quick Check:

    Missing semicolon = C [OK]
Hint: Every PHP statement ends with a semicolon [OK]
Common Mistakes:
  • Forgetting semicolons after statements
  • Thinking braces are always required
  • Changing loop condition unnecessarily
5. You want to print all even numbers from 2 to 10 using a loop in PHP. Which code correctly does this?
hard
A. for ($i = 1; $i <= 10; $i++) { if ($i % 2 == 1) echo $i; }
B. for ($i = 2; $i <= 10; $i += 2) { echo $i . ' '; }
C. while ($i = 2; $i <= 10; $i++) { echo $i; }
D. foreach (range(2, 10) as $i) { echo $i * 2; }

Solution

  1. Step 1: Identify correct loop for even numbers

    for ($i = 2; $i <= 10; $i += 2) { echo $i . ' '; } uses a for loop starting at 2, increasing by 2, printing even numbers correctly.
  2. Step 2: Check other options for errors

    for ($i = 1; $i <= 10; $i++) { if ($i % 2 == 1) echo $i; } prints odd numbers, C has wrong while syntax, D multiplies numbers incorrectly.
  3. Final Answer:

    for ($i = 2; $i <= 10; $i += 2) { echo $i . ' '; } -> Option B
  4. Quick Check:

    Increment by 2 prints evens = B [OK]
Hint: Increment by 2 to print even numbers [OK]
Common Mistakes:
  • Using wrong loop syntax
  • Printing odd numbers by mistake
  • Multiplying instead of selecting evens
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard