php how to make value 5 become array 1,2,3,4,5

It’s possible to make array from single number in PHP?
I have value $val = 5; and I want to convert to array as $val = [1,2,3,4,5].
I try like this but return empty array.

$val = 5;
for ($i = 0; $i < $val; $i++;) {
   $newArray = [
    'no' => $val[$i],
   ];
}

There is any function in PHP to achieve it?

>Solution :

Yes, you can achieve it by using range():

$val = 5;
$newArray = range(1, $val); # start = 1, end = $val

More info: https://www.php.net/manual/en/function.range.php

Leave a Reply