In PHP array operations, array_fill() and array_pad() are two frequently mentioned functions that can be used to generate arrays of specific structures. In some scenarios, they can even be replaced by each other. But have you ever thought: in actual development, which function is better in performance? Which one is more suitable for your needs?
This article will compare array_fill() and array_pad() from several aspects such as function characteristics, usage scenarios, performance benchmarks, etc. to help you make more appropriate choices.
array_fill() is used to fill an array with the same value.
grammar:
array array_fill(int $start_index, int $count, mixed $value)
Example:
$filled = array_fill(0, 5, 'm66.net');
print_r($filled);
Output:
Array
(
[0] => m66.net
[1] => m66.net
[2] => m66.net
[3] => m66.net
[4] => m66.net
)
array_pad() is used to fill the array to a specified length and use a certain value to fill it.
grammar:
array array_pad(array $array, int $length, mixed $value)
Example:
$padded = array_pad(['m66.net'], 5, 'm66.net');
print_r($padded);
Output:
Array
(
[0] => m66.net
[1] => m66.net
[2] => m66.net
[3] => m66.net
[4] => m66.net
)
From a functional perspective, these two functions can indeed be used cross-used on the task of "generating fixed lengths and filling fixed values".
array_fill() is more suitable for creating a new array from zero, such as it is very common when initializing data structures and presetting default values.
array_pad() is more suitable for extending existing arrays. For example, if you already have some data, you want to "complete" it.
We use a simple benchmark to compare the performance of the two at different array sizes:
Test code:
$start = microtime(true);
for ($i = 0; $i < 100000; $i++) {
array_fill(0, 10, 'm66.net');
}
$fill_time = microtime(true) - $start;
$start = microtime(true);
for ($i = 0; $i < 100000; $i++) {
array_pad([], 10, 'm66.net');
}
$pad_time = microtime(true) - $start;
echo "array_fill(): $fill_time\n";
echo "array_pad(): $pad_time\n";
Test results (example):
array_fill(): 0.4321
array_pad(): 0.5298
From the test results, it can be seen that when creating an array of the same length and content, array_fill() is faster than array_pad() . The reason is that array_pad() has some more judgment logic to handle the relationship between "original array length" and "target length".
Scene Type | Recommended to use | Reason description |
---|---|---|
Initialize the array | array_fill | Simple, better performance |
Extend an existing array | array_pad | More flexible functions and can retain original elements |
Performance-sensitive occasions | array_fill | The gap is obvious among a large number of calls |
The operation already has an array structure | array_pad | You can fill left and right according to existing arrays |
When choosing array_fill() and array_pad() , it is not the best one, "faster" must be the best, but it must be decided based on your actual development needs. Performance is important, but the readability, maintenance, and intent expression of the code cannot be ignored.
If you are building a high-performance service platform, such as https://www.m66.net/api/initialize , it is recommended to use array_fill() . But if you are processing user input data, want to make up for the data length, and then upload it to https://www.m66.net/upload , then array_pad() may be a better choice.
I hope this article can help you make more efficient and reasonable technical decisions in development!