0
0
PHPprogramming~10 mins

Nested loop execution in PHP - Interactive Code Practice

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

Complete the code to print numbers 1 to 3 using a for loop.

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

Complete the code to print a 3x3 grid of stars using nested loops.

PHP
<?php
for ($i = 1; $i <= 3; $i++) {
    for ($j = 1; $j [1] 3; $j++) {
        echo "* ";
    }
    echo "<br>";
}
?>
Drag options to blanks, or click blank then click option'
A>=
B<
C>
D<=
Attempts:
3 left
💡 Hint
Common Mistakes
Using < will print only 2 stars per line.
3fill in blank
hard

Fix the error in the nested loop to print numbers 1 to 3 in each row.

PHP
<?php
for ($i = 1; $i <= 3; $i++) {
    for ($j = 1; $j [1] 3; $j++) {
        echo $j . " ";
    }
    echo "<br>";
}
?>
Drag options to blanks, or click blank then click option'
A<
B<=
C>
D>=
Attempts:
3 left
💡 Hint
Common Mistakes
Using < will miss printing the number 3.
4fill in blank
hard

Fill both blanks to create a nested loop that prints a 4x4 grid of numbers.

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

Fill all three blanks to create a nested loop that prints a multiplication table from 1 to 3.

PHP
<?php
for ($i = 1; $i [1] 3; $i++) {
    for ($j = 1; $j [2] 3; $j++) {
        echo $i [3] $j . " ";
    }
    echo "<br>";
}
?>
Drag options to blanks, or click blank then click option'
A<=
B<
C*
D+
Attempts:
3 left
💡 Hint
Common Mistakes
Using < will miss the last row or column.
Using + instead of * will add numbers instead of multiplying.