I am extremely new at php and I was wondering if someone could help me use either a for()
or while()
loop to create an array of 10 elements.

- 43,625
- 12
- 83
- 136
-
Ahh, the good old days ... if this question had been asked like this today, the user would be banned - – user2835653 Sep 27 '20 at 18:00
-
@user2835653 I hope the user would be asked, "What have you tried so far?" instead of banned right away. At least that's how I would have approached it. – crazymatt Oct 28 '20 at 19:28
5 Answers
$array = array();
$array2 = array();
// for example
for ($i = 0; $i < 10; ++$i) {
$array[] = 'new element';
}
// while example
while (count($array2) < 10 ) {
$array2[] = 'new element';
}
print "For: ".count($array)."<br />";
print "While: ".count($array2)."<br />";

- 82,995
- 21
- 120
- 115
-
Attributing keys in an array via $array[] = ...; is extremely slow. It's worth having the $i from the 'for' between the square braces. $array[$i] will give you a good boost in performance when compared to $array[] if you're attributing large datasets. – I GIVE TERRIBLE ADVICE Nov 12 '08 at 03:41
-
-
3@I GIVE... i found this to be incorrect, i ran a test based on up to 750,000 iterations and found $array[] to be faster than $array[$i] in all cases (granted the difference was a matter of thousandths of a second) – Owen Nov 12 '08 at 06:20
A different approach to the for
loop would be...
$array = array();
foreach(range(0, 9) as $i) {
$array[] = 'new element';
}
print_r($array); // to see the contents
I use this method, I find it's easier to glance over to see what it does.
As strager pointed out, it may or may not be easier to read to you. He/she also points out that a temporary array is created, and thus is slightly more expensive than a normal for loop. This overhead is minimal, so I don't mind doing it this way. What you implement is up to you.

- 479,566
- 201
- 878
- 984
-
It'd be easy to read if you have a background in Python, but if you're from a C-style language background (in syntax, e.g. C++, Java, C#) this'll look strange to you. Also, the code looks inefficient, creating a temporary array. – strager Dec 17 '08 at 05:06
-
there are two inefficiencies one with using a foreach loop and a range statement where a simple for loop will suffice, the other is in using the array_push method which is 200% slower than the array[] statement (http://stackoverflow.com/questions/559844/whats-better-to-use-in-php-array-value-or-array-pusharray-value) – ejectamenta Jan 13 '16 at 13:14
a bit easier to comprehend for a beginner maybe...
<?php
// for loop
for ($i = 0; $i < 10; $i++) {
$myArray[$i] = "This is element ".$i." in the array";
echo $myArray[$i];
}
//while loop
$x = 0;
while ($x < 10) {
$someArray[$x] = "This is element ".$x." in the array";
echo $someArray[$x];
$x++;
}
?>

- 23,735
- 11
- 56
- 82
I'm not sure exactly what your purpose is here. PHP's arrays are dynamic, meaning that you can keep adding elements to them after they're created - that is, you don't need to define the length of the array at the start. I'll assume you want want to put 10 arbitrary things in an array.
for loop:
$arr = array();
for ($i = 0; $i < 10; ++$i) {
$arr[] = "Element $i";
}
while loop:
$arr = array();
$i = 10;
while (--$i) {
$arr[] = "Element $i";
}
by defining it:
$arr = array("Element 1", "Element 2", "Element 3" ...);
Or if you just wanted a range of letters or numbers:
$arr = range(0, 9);
$arr = range('a', 'j');

- 537,072
- 198
- 649
- 721
The simplest way is to use array_fill()
:
$array = array_fill(0, 10, 'Hello World');
But you should know that PHP arrays can be resized whenever you want anyway, I've never needed to create an array of a certain size.

- 88,666
- 34
- 128
- 138