Encodera

Thanks for reading!

Donate to the site — we work hard for you.

PHP reference

array_fill

Creates an array of identical values.

Syntax

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

Parameters

ParameterTypeDescription
$start_indexintThe key of the first element.
$countintThe number of elements.
$valuemixedThe value of every element.

Return value

arrayThe filled array.

Examples

Five zeros:

<?php
 
echo implode(', ', array_fill(0, 5, 0));

Output:

0, 0, 0, 0, 0

Question marks for a database query:

<?php
 
$ids = [3, 7, 9];
$marks = implode(', ', array_fill(0, count($ids), '?'));
 
echo "WHERE id IN ($marks)";

Output:

WHERE id IN (?, ?, ?)

Notes

To fill an array from a list of keys, use array_fill_keys().

See also

  • range — Creates an array holding a sequence of values.
  • str_repeat — Repeats a string a given number of times.

In the tutorial

  • Arrays — Lists and associative arrays in PHP — creating, changing, iterating and useful functions.

Thanks for reading!

Donate to the site — we work hard for you.