Answer & Solution
foreach loop
to iterate over the elements of the $colors
array and prints each element preceded by the text 'Color:'. Therefore, the output will be 'Color: Red', 'Color: Green', and 'Color: Blue' on separate lines.
foreach ($colors as $color) {
echo "Color: $color
";
}
?> | PHP | MYTAT">
foreach ($colors as $color) {
echo "Color: $color
";
}
?> | PHP | MYTAT">
<?php
$colors = array('Red', 'Green', 'Blue');
foreach ($colors as $color) {
echo 'Color: $color
';
}
?>
foreach loop
to iterate over the elements of the $colors
array and prints each element preceded by the text 'Color:'. Therefore, the output will be 'Color: Red', 'Color: Green', and 'Color: Blue' on separate lines.
What is the output of the following PHP code?<?php
for ($i = 1; $i <= 3; $i++) {
echo 'The number is: $i
';
}
?>
A). The number is: 1
The number is: 2
The number is: 3
B). The number is: 3
The number is: 2
The number is: 1
C). The number is: 1
D). The number is: 3
Which PHP control structure is used to evaluate multiple conditions and execute a block of code based on the first condition that is true?
A). if-else statement
B). for loop
C). switch statement
D). while loop
What does the following PHP code snippet do?<?php
$x = 1;
while ($x <= 5) {
echo 'The number is: $x
';
$x++;
}
?>
A). It prints numbers from 1 to 5
B). It prints numbers from 5 to 1
C). It prints 'The number is: 5' repeatedly
D). It prints 'The number is: 1' repeatedly
Which control structure in PHP is used to execute a block of code if a specified condition is true, and another block of code if the condition is false?
A). if-else statement
B). for loop
C). switch statement
D). while loop
What is the output of the following PHP code?<?php
$x = 0;
while ($x < 3) {
echo 'The number is: $x
';
$x++;
}
?>
A). The number is: 0
The number is: 1
The number is: 2
B). The number is: 3
C). The number is: 0
D). The number is: 1
What is the output of the following PHP code?<?php
$i = 1;
while ($i <= 3) {
echo 'The number is: $i
';
$i++;
if ($i == 2) {
continue;
}
}
?>
A). The number is: 1
The number is: 2
The number is: 3
B). The number is: 1
The number is: 3
C). The number is: 2
The number is: 3
D). The number is: 1
What is the purpose of the continue
statement in PHP?
A). To exit a loop or switch statement prematurely
B). To skip the current iteration of a loop
C). To restart the current iteration of a loop
D). To continue to the next iteration of a loop
Which PHP loop is typically used for iterating over the elements of an array?
A). for loop
B). while loop
C). do-while loop
D). foreach loop
What is the purpose of the break
statement in PHP?
A). To exit a loop or switch statement prematurely
B). To skip the current iteration of a loop
C). To restart the current iteration of a loop
D). To continue to the next iteration of a loop
What is the output of the following PHP code?<?php
$x = 5;
if ($x > 3) {
echo 'Hello';
} else {
echo 'Goodbye';
}
?>
A). Hello
B). Goodbye
C). Nothing
D). Error