0
0
PHPprogramming~10 mins

For loop execution model 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 from 1 to 5 using a for loop.

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

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

PHP
<?php
$sum = 0;
for ($j = 1; $j [1] 10; $j++) {
    $sum += $j;
}
echo $sum;
?>
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 sum.
Using '>' or '>=' will not run the loop as intended.
3fill in blank
hard

Fix the error in the for loop condition to count down from 5 to 1.

PHP
<?php
for ($k = 5; $k [1] 1; $k--) {
    echo $k . " ";
}
?>
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 not to run.
Using '>' without '=' will miss the last number 1.
4fill in blank
hard

Fill both blanks to create a for loop that prints even numbers from 2 to 10.

PHP
<?php
for ($m = [1]; $m [2] 10; $m += 2) {
    echo $m . " ";
}
?>
Drag options to blanks, or click blank then click option'
A2
B1
C<=
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Starting at 1 will print odd numbers.
Using '<' will exclude 10 from the output.
5fill in blank
hard

Fill all three blanks to create a for loop that prints numbers from 10 down to 2, stepping by 2.

PHP
<?php
for ($n = [1]; $n [2] 2; $n [3] 2) {
    echo $n . " ";
}
?>
Drag options to blanks, or click blank then click option'
A10
B>=
C-=
D<=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<=' will cause the loop not to run.
Using '+=' will increase the value instead of decreasing.