echo $colors[1];
? | PHP | MYTAT"> echo $colors[1];
? | PHP | MYTAT">
Q
What is the output of the following PHP code?
<?php
$colors = array('Red', 'Green', 'Blue');
echo $colors[1];
?

Answer & Solution

Answer: Option B
Solution:
The output of the code will be 'Green' because indexed arrays in PHP use integer indices to access elements, starting from zero. In this case, $colors[1] refers to the second element of the array, which is 'Green'.
Related Questions on Average

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 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
$colors = array('Red', 'Green', 'Blue');
array_push($colors, 'Yellow', 'Orange');
foreach ($colors as $color) {
echo $color . ' ';
}
?

A). Red Green Blue Yellow Orange

B). Yellow Orange Red Green Blue

C). Green Blue Red Yellow Orange

D). Yellow Orange

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()

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

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

A). remove()

B). pop()

C). delete()

D). unset()

What will be the output of the following PHP code?
<?php
$colors = array('Red' => 1, 'Green' => 2, 'Blue' => 3);
asort($colors);
foreach ($colors as $color => $value) {
echo '$color: $value ';
}
?

A). Red: 1 Green: 2 Blue: 3

B). 1: Red 2: Green 3: Blue

C). 1: Blue 2: Green 3: Red

D). Red: 3 Green: 2 Blue: 1

What will be the output of the following PHP code?
<?php
$arr1 = array('a', 'b', 'c');
$arr2 = array(1, 2, 3);
$result = array_merge($arr1, $arr2);
foreach ($result as $item) {
echo $item . ' ';
}
?

A). a b c 1 2 3

B). 1 2 3 a b c

C). a b c

D). 1 2 3

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 type of index do indexed arrays in PHP use?

A). String

B). Integer

C). Key-value pairs

D). Floating-point number