Backend Development 5 min read

Understanding PHP's array_fill() Function: Syntax, Parameters, Return Values, and Examples

This article explains PHP's array_fill() function, detailing its purpose, three parameters, return behavior, and providing both simple and multidimensional code examples to help developers quickly create and populate arrays with a single value.

php中文网 Courses
php中文网 Courses
php中文网 Courses
Understanding PHP's array_fill() Function: Syntax, Parameters, Return Values, and Examples

PHP is a widely used programming language for web development, and its functions, such as array_fill() , help improve code readability, maintainability, and reusability.

1. Function Overview

The array_fill() function fills an array with a specified value, requiring three arguments: the start index, the number of elements, and the value to fill.

2. Function Parameters

The function has three parameters:

start_index : the starting index for filling, must be a non‑negative integer.

num : the number of elements to fill, must be a non‑negative integer.

value : the value to fill, can be of any type.

3. Function Return Value

array_fill() returns a new array containing the filled elements. If start_index or num is negative, the function returns false . If num is zero, an empty array is returned.

4. Function Example

Below is a simple example demonstrating 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, start_index is 0 (starting from the first position), num is 5 (filling five elements), and value is 10 (the value used for each element), resulting in an array of five tens.

A slightly more complex example creates a two‑dimensional array filled with the value 1:

<?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 first passes 0 as the start index, then uses a nested array_fill() to create an inner array of three ones, which is then replicated three times to form a 3×3 matrix of ones.

5. Summary

The array_fill() function allows developers to quickly create a new array and populate every element with the same value, which is especially useful in PHP programming. Properly setting its parameters and understanding its return values are essential for effective use.

Practicing with various examples will help you master the function and improve your coding skills.

BackendPHPTutorialfunctionsarray_fill
php中文网 Courses
Written by

php中文网 Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.