Bird
0
0

What will be the output of this PHP code?

medium📝 Predict Output Q4 of 15
PHP - Arrays
What will be the output of this PHP code?
$colors = ['red', 'green', 'blue'];
echo $colors[1];
Agreen
BError
Cblue
Dred
Step-by-Step Solution
Solution:
  1. Step 1: Understand array indexing in PHP

    Arrays start at index 0, so $colors[0] = 'red', $colors[1] = 'green'.
  2. Step 2: Identify the output of echo $colors[1]

    This prints the second element, which is 'green'.
  3. Final Answer:

    green -> Option A
  4. Quick Check:

    Index 1 value = green = D [OK]
Quick Trick: Array indexes start at zero in PHP [OK]
Common Mistakes:
  • Thinking array starts at 1
  • Expecting error for valid index
  • Confusing element positions

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes