PHP array_fill() Function | PHP Array Functions

The array_fill() function fills an array with a specified value, starting at a given index and for a defined number of elements. This is useful for creating arrays with default values.

Key Features of array_fill()

Syntax of array_fill()

Syntax

array array_fill(int $start_index, int $count, mixed $value);

Example Using array_fill()

The following example demonstrates how array_fill() works:

Example


$array = array_fill(3, 5, "PHP");
        
print_r($array);
        

Output

Array ( [3] => PHP [4] => PHP [5] => PHP [6] => PHP [7] => PHP )