tags:

views:

231

answers:

2

What's the fastest way to populate an array with the numbers 1-100 in PHP? I want to avoid doing something like this:

$numbers = '';

for($var i = 0; i <= 100; $i++) {
    $numbers = $i . ',';
}

$numberArray = $numbers.split(',');

It seems long and tedious, is there a faster way?

+17  A: 

$var = range(0, 100);

Jason246
Thanks Jason, that's perfect!
Mr. Smith
+7  A: 

range() would work well, but even with the loop, I'm not sure why you need to compose a string and split it - what's wrong with simply:

$numberArray = array();
for ($i = 0; $i < 100; $i++)
  $numberArray[] = $i;
Guss
The code was just an example, nonetheless your way is of course much more efficient than mine :)
Mr. Smith