The Ultimate Guide to PHP's array_fill Function
The array_fill function in PHP is a powerful tool for creating arrays filled with values of your choice. In this guide, we'll take a deep dive into how to use
The array_fill function in PHP is a powerful tool for creating arrays filled with values of your choice. In this guide, we'll take a deep dive into how to use this function and how it can be applied in real-world scenarios.
What is the array_fill Function in PHP?
The array_fill function is a built-in PHP function that returns a new array filled with a given value. It is useful whenever you need an array of a known size pre-populated with the same default — for example, a grid of zeros, a list of placeholders, or a buffer you will overwrite later. Rather than writing a loop, you describe the array in a single call.
Syntax
array_fill(int $start_index, int $count, mixed $value): arrayIt takes three arguments:
$start_index— the key of the first element. If it is0or positive, the array is filled with consecutive integer keys starting from this value. As of PHP 8.0, a negative start index also fills consecutive keys (-2,-1,0, …); in PHP 7 the second key onward started from0.$count— the number of elements to create. It must be0or greater. A value of0returns an empty array, and a negative count throws aValueError(PHP 8) or raises a warning (PHP 7).$value— the value copied into every element. It can be any type: a string, a number, a boolean, an object, or even another array.
The function returns a new array; it never modifies an existing one.
How to Use the array_fill Function in PHP
Using the array_fill function is straightforward. Simply specify the start index, number of elements, and the value you want to fill the array with, and the function will return an array with the specified values.
Here's an example of how to use the array_fill function to create an array of 10 elements, starting at index 0, filled with the value "PHP":
PHP Example of array_fill function usage
The resulting array will look like this:
Array (
[0] => PHP
[1] => PHP
[2] => PHP
[3] => PHP
[4] => PHP
[5] => PHP
[6] => PHP
[7] => PHP
[8] => PHP
[9] => PHP
)Real-World Applications of the array_fill Function in PHP
The array_fill function can be used in a variety of real-world scenarios. Here are some examples:
Initializing Arrays
One of the most common uses for the array_fill function is to initialize arrays with default values. For example, you could use the function to create an array of default values for a form field.
Creating Multidimensional Arrays
The array_fill function can also be used to create multidimensional arrays. For example, you could use the function to create a 2D array filled with zeros:
PHP multidimensional usage of the array_fill function
This will create a 10x10 array filled with zeros.
Note:
array_fillstores references to the same inner array for each row. Modifying one row will modify all rows. To create independent rows, use a loop to generate separate inner arrays.
Filling Arrays with a Single Value
The array_fill function repeats a single value across multiple indices. For example, you could use it to create an array of 10 elements starting at index 1, all filled with the value 5:
PHP Example fill an array with a repeated value
This will create an array with 10 elements, starting at index 1, filled with the value 5.
Note: To generate an array with incremental values (e.g., numbers 1 to 10), use the built-in
range(1, 10)function instead.
Related Functions
array_fill is one of several PHP functions for building arrays without a loop. Depending on what you need, a sibling function may fit better:
array_fill_keys()— fill an array using a list of keys you provide, instead of consecutive integers.range()— build an array of incremental values (e.g.1, 2, 3, …), whicharray_fillcannot do.array_pad()— extend an existing array to a given size by padding it with a value.array_map()— transform a filled array (for example, turn a row of zeros into computed values).str_repeat()— the string equivalent when you want a repeated value as text rather than as array elements.
Conclusion
The array_fill function gives you a concise, loop-free way to create an array of a known size pre-filled with a single value. It shines when initializing arrays with defaults, building grids of zeros, or preparing buffers you will later overwrite. Remember its boundaries: the count must not be negative, the same value is copied into every element, and for incremental sequences you should reach for range() instead. With these rules in mind, array_fill is a clean tool for predictable array setup in PHP.