What will be the output of the following PHP code?

<?php
$data = [[1, 2], [3]];
echo count($data) . " ";
echo count($data, COUNT_RECURSIVE);
?>
                        

Options

  • A. 3 3
  • B. 2 5
  • C. 5 2
  • D. More than one of the above
  • E. None of the above

Correct Answer (Detailed Explanation is Below)

B. 2 5

Detailed Explanation

Correct Answer: Option B

Explanation:
count($data) counts only the two top-level elements, giving 2. count($data, COUNT_RECURSIVE) also counts elements inside the nested arrays: the outer array has 2 elements, and the inner arrays contain 2 and 1 elements. The total is 2 + 2 + 1 = 5.