Bird
0
0

Given $text = "abcdefg", which code extracts every second character starting from index 1 using substring extraction in a loop?

hard📝 Application Q9 of 15
PHP - String Functions
Given $text = "abcdefg", which code extracts every second character starting from index 1 using substring extraction in a loop?
Afor ($i = 0; $i < strlen($text); $i += 2) { echo substr($text, $i, 2); }
Bfor ($i = 1; $i < strlen($text); $i += 2) { echo substr($text, $i, 1); }
Cfor ($i = 1; $i <= strlen($text); $i++) { echo substr($text, $i, 1); }
Dfor ($i = 1; $i < strlen($text); $i++) { echo substr($text, $i, 2); }
Step-by-Step Solution
Solution:
  1. Step 1: Loop from index 1 with step 2

    Start at 1 to get second character, increment by 2 to get every second character.
  2. Step 2: Extract one character at each position

    Use substr with length 1 to get single character at current index.
  3. Final Answer:

    for ($i = 1; $i < strlen($text); $i += 2) { echo substr($text, $i, 1); } -> Option B
  4. Quick Check:

    Loop with step 2 and substr length 1 extracts every second char [OK]
Quick Trick: Loop with step 2, substr length 1 extracts chars one by one [OK]
Common Mistakes:
  • Using wrong step size
  • Extracting length 2 instead of 1
  • Looping beyond string length

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes