Bird
0
0

What will be the output of this PHP code?

medium📝 Predict Output Q5 of 15
PHP - String Functions
What will be the output of this PHP code?
$str = "OpenAI ChatGPT";
echo substr($str, -6, 4);
AChat
BhatG
CGPT
DAI C
Step-by-Step Solution
Solution:
  1. Step 1: Understand negative start index

    Start at -6 means counting 6 characters from the end: "ChatGPT" starts at index -7, so -6 points to 'h'.
  2. Step 2: Extract 4 characters from that position

    Characters are 'h', 'a', 't', 'G' forming "hatG".
  3. Final Answer:

    hatG -> Option B
  4. Quick Check:

    Negative start with length extracts correct substring [OK]
Quick Trick: Negative start counts from string end backwards [OK]
Common Mistakes:
  • Starting at wrong character with negative index
  • Confusing length parameter
  • Expecting substring to start at positive index

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes