Understanding PHP's array_fill() Function: Syntax, Parameters, Return Values, and Examples
This article explains PHP's array_fill() function, detailing its purpose, parameters, return behavior, and providing clear code examples—including simple and two‑dimensional array usage—to help developers quickly create and populate arrays in backend development.
PHP is a very popular programming language, widely used in web development. Functions are an important component of PHP, helping programmers improve code readability, maintainability, and reusability. Among PHP's function library, the useful function array_fill() will be introduced in detail.
1. Function Overview
The array_fill() function fills an array by using the same value for each element. It requires three parameters: the start index, the number of elements, and the value to fill.
2. Function Parameters
The array_fill() function has three parameters, whose purposes are as follows:
start_index : the starting index position to fill, which must be a non‑negative integer.
num : the number of elements to fill, which must be a non‑negative integer.
value : the value to fill, which can be of any type.
3. Function Return Value
The array_fill() function returns a new array containing the filled elements. If start_index is negative or num is negative, the function returns false . Otherwise it fills the array according to the specified start_index and num . If num is 0, the function returns an empty array.
4. Function Examples
Below is a simple example to help understand the usage of array_fill() :
<?php
$array1 = array_fill(0, 5, 10);
print_r($array1);
?>Output:
Array ( [0] => 10 [1] => 10 [2] => 10 [3] => 10 [4] => 10 )In this example, we use array_fill() to create a new array. The start_index is 0, meaning filling starts from the first position; num is 5, meaning five elements are created; value is 10, so each element is filled with 10. The result is an array of five elements, each containing 10.
Now a slightly more complex example creates a two‑dimensional array, filling each element of the first and second layers with a numeric value:
<?php
$array2 = array_fill(0, 3, array_fill(0, 3, 1));
print_r($array2);
?>Output:
Array ( [0] => Array ( [0] => 1 [1] => 1 [2] => 1 )
[1] => Array ( [0] => 1 [1] => 1 [2] => 1 )
[2] => Array ( [0] => 1 [1] => 1 [2] => 1 ) )This example demonstrates that the first parameter of array_fill() is 0, indicating filling starts from the first position. The second parameter is 3, creating an array of three elements, each of which is itself an array filled with the value 1, resulting in a 3×3 matrix of ones.
5. Summary
The array_fill() function helps programmers quickly create a new array and fill each element with the same value. It is very useful in PHP programming. Pay attention to the correct setting of parameters and understand the return values to master its usage.
We recommend practicing more function examples to better grasp their usage and characteristics, thereby improving your programming skills.
Java learning material download
C language learning material download
Frontend learning material download
C++ learning material download
PHP learning material download
php中文网 Courses
php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.