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

Answer & Solution

Answer: Option A
Solution:
The foreach loop is commonly used in PHP to iterate over elements of an array, as it provides a simple and concise syntax for iterating through all elements of an array without needing to know its length or explicitly managing the index variable.
Related Questions on Average

What will be the output of the following PHP code?
<?php
$info = array('name' => 'John', 'age' => 30, 'city' => 'New York');
echo array_key_exists('age', $info) ? 'Yes' : 'No';
?

A). Yes

B). No

C). John

D). 30

What will be the output of the following PHP code?
<?php
$numbers = array(10, 20, 30, 40, 50);
array_pop($numbers);
foreach ($numbers as $number) {
echo $number . ' ';
}
?

A). 20 30 40 50

B). 10 20 30 40

C). 10 20 30 40

D). 10 20 30 40 50

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

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 is the output of the following PHP code?
<?php
$colors = array('Red', 'Green', 'Blue');
echo $colors[1];
?

A). Red

B). Green

C). Blue

D). 1

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

A). remove()

B). pop()

C). delete()

D). unset()

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

A). To check if a specified key exists in an array

B). To check if a specified value exists in an array

C). To retrieve all keys from an array

D). To retrieve all values from an array

Which of the following statements is true about associative arrays in PHP?

A). They only allow numeric indices

B). They store elements in a sequential order

C). They use key-value pairs to access elements

D). They cannot be accessed using loop constructs

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

A). String

B). Integer

C). Key-value pairs

D). Floating-point number