Bird
0
0

Given the string $sentence = 'The quick brown fox jumps', which code snippet correctly extracts the substring starting from the word 'brown' using strstr?

hard📝 Application Q15 of 15
PHP - String Functions
Given the string $sentence = 'The quick brown fox jumps', which code snippet correctly extracts the substring starting from the word 'brown' using strstr?
Aecho strstr($sentence, 'brown');
Becho strstr($sentence, 'brown', true);
Cecho strpos($sentence, 'brown');
Decho substr($sentence, strpos($sentence, 'brown'));
Step-by-Step Solution
Solution:
  1. Step 1: Understand strstr behavior

    strstr returns the part of the string starting from the first occurrence of the substring, including it.
  2. Step 2: Analyze options

    echo strstr($sentence, 'brown'); correctly uses strstr to get substring from 'brown'. echo strstr($sentence, 'brown', true); returns part before 'brown'. echo strpos($sentence, 'brown'); returns position, not substring. echo substr($sentence, strpos($sentence, 'brown')); uses substr with strpos, which also works but is not strstr.
  3. Final Answer:

    echo strstr($sentence, 'brown'); -> Option A
  4. Quick Check:

    strstr returns substring from needle [OK]
Quick Trick: strstr returns substring from needle, true returns before [OK]
Common Mistakes:
  • Using third parameter true to get substring after needle
  • Confusing strpos with strstr
  • Expecting strpos to return substring

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes