Bird
0
0

What will be the output of the following PHP code?

medium📝 Predict Output Q13 of 15
PHP - Arrays
What will be the output of the following PHP code?
$fruits = ['apple', 'banana', 'cherry'];
echo $fruits[1];
Acherry
Bapple
Cbanana
DError
Step-by-Step Solution
Solution:
  1. Step 1: Understand array indexing in PHP

    Arrays in PHP start indexing at 0. So, $fruits[0] is 'apple', $fruits[1] is 'banana'.
  2. Step 2: Identify the output of echo $fruits[1]

    The code prints the element at index 1, which is 'banana'.
  3. Final Answer:

    banana -> Option C
  4. Quick Check:

    Index 1 = banana [OK]
Quick Trick: Arrays start at zero index [OK]
Common Mistakes:
  • Assuming arrays start at index 1
  • Confusing element values
  • Expecting an error for valid index

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes