Current Location: Home> Latest Articles> array_fill() is the filled value referenced or copied?

array_fill() is the filled value referenced or copied?

M66 2025-06-05

In PHP programming, array_fill() is a very practical function to create and fill an array. Its function signature is as follows:

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

The function is to start at the specified starting index position, create an array containing $count elements, and fill it with the same $value . A common question is: Is this $value copied into an array by value, or is it passed by reference?

The behavior of filling values: Copy by value

The array_fill() function is filled by copy by value by default. That is, if you are populating a reference to an object or array, each element will be a separate copy in array_fill() , rather than multiple elements sharing the same reference.

Let’s take a look at an example:

 $obj = new stdClass();
$obj->name = "Alice";

$filled = array_fill(0, 3, $obj);
$filled[0]->name = "Bob";

echo $filled[1]->name; // Output "Bob"

You will find that although array_fill() is passed by value, objects exist in reference semantics, so each item in the array points to the same object. When you modify the properties of one of the objects, the other items are also affected.

But if it is filled with a normal data type, such as an integer or a string, then each array element is an independent value:

 $filled = array_fill(0, 3, "Hello");
$filled[0] = "World";

print_r($filled);
// Output:
// Array
// (
//     [0] => World
//     [1] => Hello
//     [2] => Hello
// )

In this example, modifying $filed[0] will not affect other elements, indicating that the string is copied by value.

How to fill by reference?

If you want each array element to reference the same variable, not just a copy of the value, you can use array_fill() with reference to the variable. Examples are as follows:

 $value = "shared";
$filled = array_fill(0, 3, &$value);

$value = "updated";
print_r($filled);
// Output:
// Array
// (
//     [0] => updated
//     [1] => updated
//     [2] => updated
// )

In this example, using &$value explicitly as a reference, each element in the array is bound to the same variable $value . Any changes to $value are reflected synchronously to each item in the array.

But be aware that this method is not common in PHP and is also prone to cause unexpected behavior. Generally, it is recommended only if multiple array items really need to share the same reference.

Summarize

  • The default behavior of array_fill() is to copy by value .

  • If $value is an object or an array, the copied one is actually a reference (because the object is always passed in reference in PHP).

  • You can force a reference to use &$value to implement a true shared reference.

  • Use references with caution to avoid causing difficult debugging problems.

Correct understanding of the filling mechanism of array_fill() helps avoid potential logical errors in actual projects. If you are developing functional modules similar to cache initialization, placeholder structure, and batch configuration, it is particularly important to master these differences.

For example:

 $placeholders = array_fill(0, 10, "http://m66.net/placeholder");

The above code will populate an array of 10 fixed URLs that are copied from values ​​and will not share references.

By understanding these underlying mechanisms, you can more accurately control the behavior of the array and improve the robustness and maintainability of your code.