Bird
0
0

Which of the following is the correct syntax to extract a substring from a string $str starting at position 4 with length 5 in PHP?

easy📝 Syntax Q12 of 15
PHP - String Functions
Which of the following is the correct syntax to extract a substring from a string $str starting at position 4 with length 5 in PHP?
Asubstr($str, 5);
Bsubstr($str, 4, 5);
Csubstr($str, 4);
Dsubstr($str, 5, 4);
Step-by-Step Solution
Solution:
  1. Step 1: Recall substr syntax

    substr(string, start, length) extracts substring starting at 'start' index with 'length' characters.
  2. Step 2: Match parameters to question

    Start is 4, length is 5, so substr($str, 4, 5) is correct syntax.
  3. Final Answer:

    substr($str, 4, 5); -> Option B
  4. Quick Check:

    substr($str, 4, 5) = correct syntax [OK]
Quick Trick: Syntax is substr(string, start, length) with start zero-based [OK]
Common Mistakes:
  • Swapping start and length values
  • Omitting length when needed
  • Using 1-based start index

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes