The Magic of an Empty Container: How to Make PHP Work for You
\n\nImagine: you're writing a PHP script and need to collect data — for example, product IDs from a shopping cart. You create an empty array, then add elements to it in a loop. It seems simple enough, right? But it's precisely this moment — adding elements to an empty PHP array — that generates thousands of questions on Stack Overflow. Why? Because beginners often confuse the syntax, and experienced developers want to understand what happens "under the hood."
\n\nIn this article, we won't just show you how to write the code correctly. We'll look inside the PHP core (Zend Engine), break down how $arr[] = $value differs from array_push(), and find out what surprises PHP 8.1 has in store. Ready? Let's go!
The Most Common Question: How to Add an Element?
\n\nIf you're a beginner, the first thing that likely comes to mind is to write something like:
\n\n<?php\n$myArray = array(); // empty array\n$myArray[] = 'first element';\n$myArray[] = 'second element';\nprint_r($myArray);\n?>\n\nThis code will output:
\n\nArray\n(\n [0] => first element\n [1] => second element\n)\n\nDoes it work? Yes. But why? Let's break down what the [] operator actually does.
The $arr[] = $value Operator: Fast and Elegant
\n\nWhen you write $arr[] = $value, PHP doesn't call any functions. Instead, it directly accesses the internal data structure of the array (zend_array in Zend Engine terms).
What happens under the hood:
\n- \n
- PHP checks if the array exists. If not, it creates a new one (but it's always better to initialize it explicitly:
$arr = []). \n - It adds the element to the end of the array, automatically assigning it the next numeric key (0, 1, 2...). \n
- If the array was empty, the first element gets the key
0. \n - No overhead from a function call. This is the most performant way. \n
Tip: use $arr[] = ... whenever you simply need to add an element to the end. It's fast, readable, and doesn't require importing functions.
array_push(): When You Need to Add Multiple Elements at Once
\n\nThe second popular method is the array_push() function:
<?php\n$myArray = [];\narray_push($myArray, 'first', 'second', 'third');\nprint_r($myArray);\n?>\n\nThe result will be the same. But there are nuances:
\n- \n
array_push()takes the array by reference and adds all passed values to the end. \n - It returns the new number of elements in the array (as of PHP 7.3). \n
- Under the hood: the function calls the internal procedure
zend_hash_next_index_insertfor each element. This means that if you are adding a single element,array_push()is slightly slower than$arr[] = ...due to the overhead of the function call. \n
When to use: only if you need to add multiple elements at once (2 or more). For a single element — forget about array_push, use [].
Empty Array and Keys: What if the Keys Aren't Numeric?
\n\nWhat if you want to add an element with a specific key? For example, an associative array:
\n\n<?php\n$user = [];\n$user['name'] = 'Ivan';\n$user['age'] = 30;\n?>\n\nThis is also simple. The [] operator with a key inside the square brackets ($arr['key'] = value) works as an assignment. If the key doesn't exist, it's created. If it exists, it's overwritten.
But be careful: if you accidentally write $user[] = 'value' next to an associative array, PHP will add a numeric key