foreach ($colors as $color) {
echo $color . "
";
}
?> | PHP | MYTAT"> foreach ($colors as $color) {
echo $color . "
";
}
?> | PHP | MYTAT">
Q
What is the purpose of the following PHP code?

<?php
$colors = array('Red', 'Green', 'Blue');
foreach ($colors as $color) {
echo $color . '
';
}
?>

Answer & Solution

Answer: Option A
Solution:
The code uses a foreach loop to iterate over each element of the $colors array and print it. Therefore, the purpose of the code is to loop through an array and print each element.
Related Questions on Average

What data type is the variable $age = 25; in PHP?

A). Integer

B). String

C). Boolean

D). Float

What will be the output of the following PHP code?

<?php
$name = 'John';
echo 'Hello, $name!';
?>

A). Hello, John!

B). Hello, $name!

C). Hello, !

D). Undefined variable: name

Which of the following is a correct way to declare an array in PHP?

A). $colors = array('Red', 'Green', 'Blue');

B). $colors = 'Red, Green, Blue';

C). $colors = {'Red', 'Green', 'Blue'};

D). $colors = [Red, Green, Blue];

What will be the output of the following PHP code?

<?php
$x = 10;
$y = 5;
echo $x + $y;
?>

A). 15

B). 5

C). 10

D). Error

Which of the following is a correct way to increment the value of a variable $count by 1 in PHP?

A). $count++;

B). $count += 1;

C). $count = $count + 1;

D). $count =+ 1;

What is the value of $x after the following PHP code is executed?

<?php
$x = 5;
$x += 3;
echo $x;
?>

A). 5

B). 8

C). 15

D). 3

What will be the value of the variable $result after executing the following PHP code?

<?php
$x = 5;
$result = ($x > 10) ? 'Yes' : 'No';
?>

A). Yes

B). No

C). True

D). False

What will be the result of the expression 10 % 3 in PHP?

A). 0

B). 1

C). 3

D). 10

Which of the following PHP functions is used to find the length of an array?

A). count()

B). length()

C). size()

D). sizeof()

Which of the following PHP operators is used for concatenation of strings?

A). .

B). +

C). &&

D). *