17

If I have a variable $num = 50 how can I put the numbers 1-50 into an array?

3 Answers 3

54

Take a look at the range function.

$array = range(1, $num);
Sign up to request clarification or add additional context in comments.

Comments

12

Have a look at the documentation for the range() function:

<?php

    $array = range(1, 50);

?>

Comments

4

This can be solved by using a simple for loop:

//  Start ↓    End ↓  Step ↓
for ($i = 1; $i <= $num; ++$i) {
    $array[] = $i;
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.