Complete the code to print numbers from 1 to 5 using a loop.
<?php for ($i = 1; $i [1] 5; $i++) { echo $i . "\n"; } ?>
The loop should run while $i is less than or equal to 5, so using <= with 5 makes it print 1 to 5.
Complete the code to sum numbers from 1 to 10 using a loop.
<?php $sum = 0; for ($num = 1; $num [1] 10; $num++) { $sum += $num; } echo $sum; ?>
Using <= ensures the loop includes 10 in the sum.
Fix the error in the loop condition to print even numbers from 2 to 10.
<?php for ($i = 2; $i [1] 10; $i += 2) { echo $i . "\n"; } ?>
Using <= includes 10 in the output, so even numbers 2,4,6,8,10 are printed.
Fill both blanks to create an associative array of squares for numbers 1 to 5.
<?php $squares = []; for ($i = 1; $i <= 5; $i++) { $squares[[1]] = [2]; } print_r($squares); ?>
The key should be $i and the value should be $i * $i to store squares.
Fill all three blanks to create a filtered array of numbers greater than 3 with their squares.
<?php $numbers = [1, 2, 3, 4, 5]; $result = []; foreach ($numbers as [1]) { if ([1] > 3) { $result[[2]] = [3]; } } print_r($result); ?>
Use $num as the key and $num * $num as the value for numbers greater than 3. The loop variable is $num.