menu

PHP Arrays


1. Which of the following is the correct syntax to create an array in PHP?

$array = array("apple", "banana", "orange");

$array = ["apple", "banana", "orange"];

Both A and B

None of the above


2.

What is the output of the following PHP code snippet?

<?php
$array = ["apple", "banana", "orange"];
$reversed_array = array_reverse($array);
print_r($reversed_array);
?>

["orange", "banana", "apple"]

["apple", "banana", "orange"]

["orange", "apple", "banana"]

None of the above


3. What is an array in PHP?

A collection of similar data types

A collection of different data types

A variable that can hold only one value at a time

None of the above


4. How can you access the first element of an array in PHP?

$array[0]

$array[1]

$array[first]

$array["first"]


5.

What is the output of the following PHP code snippet?

<?php
$array = ["apple", "banana", "orange"];
$last_element = end($array);
echo $last_element;
?>

apple

banana

orange

0


6.

What is the output of the following PHP code snippet?

<?php
$array1 = ["apple", "banana"];
$array2 = ["orange", "grape"];
$result = array_merge($array1, $array2);
print_r($result);
?>

["apple", "banana"]

["orange", "grape"]

[0=>"apple", 1=>"banana", 2=>"orange", 3=>"grape"]

None of the above


7.

What is the output of the following PHP code snippet?

<?php
$array = ["apple", "banana", "orange"];
echo count($array);
?>

0

1

2

3


8. Which of the following is not a valid way to loop through an array in PHP?

foreach()

for()

while()

do-while()


9. Which of the following is not a valid way to remove an element from an array in PHP?

unset()

array_splice()

array_remove()

array_shift()


10. What is the function used to add an element to the end of an array in PHP?

array_push()

array_add()

add_array()

array_append()