PHP array_fill() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Array

Definition

In PHP, array_fill() creates an array that contains the same value repeated a number of times. You choose the first key, how many items to create, and the value to use.

You use array_fill() to prepare arrays with default values, such as a list of zeros for counters or an empty grid for a game board. The keys are consecutive integers that begin at the start index you choose.

Syntax

Call array_fill() with the values you need. Start with this line.

PHP
$zeros = array_fill(0, 5, 0);
CallWhat it does
array_fill(0, 3, 'x')Returns ['x', 'x', 'x'] with the keys 0, 1 and 2.
array_fill(5, 3, 0)Returns three zeros with the keys 5, 6 and 7.
array_fill(0, 0, 'x')Returns an empty array, because the count is 0.

If you want string keys, use array_fill_keys() instead. If you want a range of numbers, use range(). array_fill() always creates integer keys that increase by 1.

Library declarations
PHP
array_fill(int $start_index, int $count, mixed $value): array

Parameters

ParameterDescription
start_indexRequired. The key of the first item. The following keys increase by 1.
countRequired. How many items to create. It must be 0 or more.
valueRequired. The value that is placed in every item. It can be any type, including an array.

Return Value

Return ValueDescription
arrayA new array with count items that all hold the given value.

PHP Examples

Example 1 — Filling an Array With One Value

Create three items that all hold the same text.

PHP
<?php
$filled = array_fill(0, 3, 'x');

print_r($filled);

How It Works

  1. The first argument 0 is the key of the first item.
  2. The second argument 3 asks for three items.
  3. The third argument 'x' is the value stored in every item.
  4. The keys increase by 1 each time, giving 0, 1 and 2.

Example 2 — A Different Start Index and a Count of 0

The start index does not need to be 0, and a count of 0 gives an empty array.

PHP
<?php
print_r(array_fill(5, 3, 0));

var_dump(array_fill(0, 0, 'x'));

How It Works

  1. The start index 5 makes the first key 5.
  2. The next two items use the keys 6 and 7.
  3. A count of 0 asks for no items at all.
  4. The second result is therefore an empty array, shown by var_dump() as array(0).

Example 3 — Creating a Grid

Use an array as the fill value to make rows of a grid. Each row is an independent copy.

PHP
<?php
$grid = array_fill(0, 2, array_fill(0, 3, 0));

$grid[0][1] = 1;

echo implode(',', $grid[0]), PHP_EOL;
echo implode(',', $grid[1]), PHP_EOL;

How It Works

  1. The inner array_fill() makes a row of three zeros.
  2. The outer array_fill() uses that row as the value for two items.
  3. Changing $grid[0][1] to 1 only changes the first row.
  4. PHP copies arrays when they are changed, so the second row stays all zeros.

Frequently Asked Questions

It creates an array with a given number of items, all holding the same value, with integer keys that begin at the start index you choose.
Use array_fill_keys(). You give it a list of keys and one value, and it assigns that value to every key.
You get an empty array. A negative count is not allowed and throws a ValueError in PHP 8.
Yes. The value can be any type. Because PHP copies arrays when they change, each filled array is independent, which is useful for grids.
array_fill() repeats one value. range() creates a sequence of different values, such as 1, 2, 3, 4.

Next: array_fill_keys()

array_fill_keys() creates an array by using the values of a list as keys and giving every .

array_fill_keys() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful