array_push($colors, "Yellow", "Orange");
foreach ($colors as $color) {
echo $color . " ";
}
? | PHP | MYTAT"> array_push($colors, "Yellow", "Orange");
foreach ($colors as $color) {
echo $color . " ";
}
? | PHP | MYTAT">
Q
What will be the output of the following PHP code?
<?php
$colors = array('Red', 'Green', 'Blue');
array_push($colors, 'Yellow', 'Orange');
foreach ($colors as $color) {
echo $color . ' ';
}
?

Answer & Solution

Answer: Option B
Solution:
The output of the code will be Red Green Blue Yellow Orange because the array_push() function is used to add the elements 'Yellow' and 'Orange' to the end of the $colors array. The foreach loop then iterates over the array, printing each color, resulting in the output Red Green Blue Yellow Orange.
Related Questions on Average

What is the purpose of the array_push() function in PHP?

A). To remove elements from an array

B). To add elements to the beginning of an array

C). To add elements to the end of an array

D). To reverse the order of elements in an array

What will be the output of the following PHP code?
<?php
$person = array('name' => 'John', 'age' => 30, 'city' => 'New York');
echo $person['age'];
?

A). name: John

B). age: 30

C). city: New York

D). John

Which PHP construct is commonly used to iterate over elements of an array?

A). foreach loop

B). while loop

C). do-while loop

D). for loop

What is the correct syntax for creating a multidimensional array in PHP?

A). $array = array(array(1, 2, 3), array(4, 5, 6), array(7, 8, 9));

B). $array = array([1, 2, 3], [4, 5, 6], [7, 8, 9]);

C). $array = array{1, 2, 3}, {4, 5, 6}, {7, 8, 9};

D). $array = array{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};

What is the correct way to access the first element of an indexed array in PHP?

A). $array[0]

B). $array[1]

C). $array['first']

D). $array['0']

What will be the output of the following PHP code?
<?php
$info = array('name' => 'Alice', 'age' => 25);
echo count($info);
?

A). 1

B). 2

C). 25

D). Alice

What will be the output of the following PHP code?
<?php
$matrix = array(
array(1, 2, 3),
array(4, 5, 6),
array(7, 8, 9)
);
echo $matrix[1][2];
?

A). 1

B). 2

C). 4

D). 6

What type of index do indexed arrays in PHP use?

A). String

B). Integer

C). Key-value pairs

D). Floating-point number

Which PHP function is used to sort an array in ascending order, maintaining the key-value pairs?

A). sort()

B). ksort()

C). asort()

D). rsort()

Which PHP function is used to remove the last element from an array?

A). remove()

B). pop()

C). delete()

D). unset()