Bird
0
0

Identify the error in this PHP code snippet:

medium📝 Debug Q14 of 15
PHP - String Functions
Identify the error in this PHP code snippet:
$data = "red|green|blue";
$colors = explode($data, "|");
print_r($colors);
AThe delimiter "|" is invalid in explode().
BThe delimiter and string parameters are swapped in explode().
Cexplode() cannot split strings with pipe characters.
Dprint_r() cannot print arrays.
Step-by-Step Solution
Solution:
  1. Step 1: Check explode() parameter order

    explode() expects delimiter first, then the string to split.
  2. Step 2: Analyze given code

    Code uses explode($data, "|"), which swaps parameters incorrectly.
  3. Final Answer:

    The delimiter and string parameters are swapped in explode(). -> Option B
  4. Quick Check:

    explode(delimiter, string) correct order [OK]
Quick Trick: Delimiter always first, string second in explode() [OK]
Common Mistakes:
  • Swapping parameters causing wrong output or errors
  • Thinking delimiter must be a letter only
  • Assuming print_r() can't print arrays

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes