array_fill
Creates an array of a given size where every element holds the same value.
array_fill(int $start_index, int $count, mixed $value): array- Added in PHP 4.2.0
Parameters
| Parameter | Type | Description |
|---|---|---|
$start_index | int | The first key of the returned array. Subsequent keys increment from there. |
$count | int | How many elements to create. Must be between 0 and 2147483647. |
$value | mixed | The value copied into every element. |
Return value
arrayAn array of $count elements holding $value, with consecutive integer keys starting at $start_index.
Errors
Throws a ValueError if $count is negative or larger than 2147483647.
Changelog
- PHP 8.0.0Throws a ValueError when $count is out of range; previously an E_WARNING was raised and the function returned false.
- PHP 8.0.0A negative $start_index now increments normally (-2, -1, 0, 1). Previously only the first key was negative and the rest started at 0.
array_fill() builds an array of a fixed size in which every element holds the same value, with consecutive integer keys starting at whatever index you name. It is the array equivalent of str_repeat().
When to reach for it
Use it to pre-size a structure so no key is ever undefined: a row of zeros for a counter or histogram, a fixed-length buffer, a board or grid built by filling rows and then filling each row, or a run of '?' placeholders for a prepared statement (implode(',', array_fill(0, count($ids), '?'))). The $start_index argument is handy when the array has to line up with 1-based data such as month numbers.
What trips people up
Every element is the same value, not a copy that is independent. For scalars that is invisible because PHP copies on write, but for objects all elements point at one shared instance — mutating $a[0] changes $a[9] too. Build object arrays with a loop or array_map() over range() instead.
Since PHP 8.0, a negative or oversized $count throws a ValueError rather than returning false, so a computed count needs validating before the call.
Example
When to use it
- Build a run of '?' placeholders matching the number of bound values in a query
- Pre-size a counter array with zeros so ++ never hits an undefined key
- Create a fixed-size grid row before filling in individual cells
Frequently asked questions
How do I create an array of a fixed length in PHP?
array_fill(0, $length, $default) returns an array of $length elements keyed from 0, each holding the default value. PHP arrays still grow dynamically afterwards — this only pre-populates them.
Why do all the objects in my filled array change together?
array_fill puts the same object reference in every slot, so modifying one modifies all of them. Build the array with array_map(fn() => new MyClass(), range(1, $n)) to get separate instances.
What is the difference between array_fill and array_fill_keys?
array_fill generates sequential integer keys starting at an index you choose, while array_fill_keys takes an existing array of key names and gives each of them the same value.
More examples
Starting at a different index
Starting at 1 keeps month numbers aligned with the array keys. The negative example shows the PHP 8 behaviour: keys run -2, -1, 0, 1, whereas PHP 7 produced -2, 0, 1, 2.
<?php
$months = array_fill(1, 12, 0);
$months[3] = 42;
echo "first key: ", array_key_first($months), "\n";
echo "march: ", $months[3], "\n";
print_r(array_fill(-2, 4, 'x'));Objects are shared, not copied
The first var_dump prints true: all three slots hold one object, so touching index 0 changed index 2. The array_map version creates a distinct object per slot and prints false.
<?php
$rows = array_fill(0, 3, new stdClass());
$rows[0]->hit = true;
var_dump(isset($rows[2]->hit));
$fresh = array_map(fn() => new stdClass(), range(1, 3));
$fresh[0]->hit = true;
var_dump(isset($fresh[2]->hit));An invalid count throws
A count of zero is legal and returns an empty array, but since PHP 8.0 a negative count is a ValueError instead of a warning plus false — worth guarding when the count comes from user input or a subtraction.
<?php
print_r(array_fill(0, 0, 'x'));
try {
array_fill(0, -1, 'x');
} catch (ValueError $e) {
echo "ValueError: ", $e->getMessage(), "\n";
}
Discussion