Bird
0
0

You want to create an indexed array of numbers from 1 to 5 in PHP without manually listing each number. Which code correctly does this?

hard📝 Application Q15 of 15
PHP - Arrays
You want to create an indexed array of numbers from 1 to 5 in PHP without manually listing each number. Which code correctly does this?
A<code>$numbers = array(1 to 5);</code>
B<code>$numbers = [1..5];</code>
C<code>$numbers = range(1, 5);</code>
D<code>$numbers = {1, 2, 3, 4, 5};</code>
Step-by-Step Solution
Solution:
  1. Step 1: Recall PHP function for range creation

    PHP provides the range() function to create arrays with sequences of numbers.
  2. Step 2: Evaluate each option

    $numbers = range(1, 5); uses range(1, 5) correctly. Options A, B, and D use invalid syntax or unsupported constructs.
  3. Final Answer:

    $numbers = range(1, 5); -> Option C
  4. Quick Check:

    Use range() for sequences [OK]
Quick Trick: Use range(start, end) to create number arrays quickly [OK]
Common Mistakes:
  • Using invalid range syntax like [1..5]
  • Trying to write '1 to 5' inside array()
  • Using curly braces {} for arrays

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes