How to Initialize an Empty Array in PHP

These are the two best ways to initialize an empty array in PHP.

Published Categorized as PHP, Web & Code

Suppose you wanted to initialize an empty array object in PHP so that you can push elements to it later on in your application. (Which, since you’re here and you’re reading this post, you probably do.)

What is the best way to do this?

Initialize an Empty Array

There are two ways to initialize an empty array in PHP: with brackets, or with the array() language construct. The two can be used interchangeably and neither is more performant than the other.

With Brackets

The simplest, most straightforward way to initialize an empty array object in PHP is to declare it with empty brackets:

<?php
$my_array = [];
?>

The result is an empty array object that you can push to, mutate, reassign, and unset as you wish.

With the array() Language Construct

As an alternative, you can also use the array() language construct:

<?php
$my_array = array();
?>

Once again, the result is an empty array object that you can push to, mutate, reassign, and unset as needed.

Which Should I Use?

Which of the two methods to use comes down to:

  1. Your team’s style guide;
  2. Your personal preferences;
  3. The PHP version running on the server.

Your team’s style guide:

If the style code for your code mandates one method over the other, stick to it. This is what keeps your code consistent, easy for others to read, and just as easy to debug and troubleshoot.

Your personal preferences:

Unlike JavaScript, where declaring the array object as a string literal gives you performance gains as you don’t have to call a constructor function, the language constructs in PHP are not necessarily treated as functions.

Simply put, in PHP declaring an empty array object with brackets is not more or less performant than using the array() language construct. As far as the PHP compiler is concerned, they are one and the same thing.

So, if the style guide lets you choose, which to use is up to you.

The PHP version that’s running on the server:

The version of PHP that’s running on your server, however, is what can tilt the scales in one direction on the other.

The first method—declaring an empty object with brackets—is only available from PHP 5.4 and above. The array() language construct, on the other hand, is available from PHP 4 and above.

Keep this in mind if you’re working on an older application. Not taking this compatibility constraint into account may give you and the rest of the devs a headache. (Why the application is based on such an old version of this language is the subject of a whole other discussion…)

Using the Array

The array in PHP, as with most other programming languages, is basically a variable that lets you store multiple values under its name.

You can mutate and reassign the array. You can also push values to the array, unset values from it, and unset the whole of it altogether (in other words, destroy it).

Pushing Values to the Array

To push one or more values to the array, do the following:

<?php
// Instantiate an empty array
$brands = [];

// Push values to indices 0, 1, and 2
$brands[] = "Apple";
$brands[] = "Google";
$brands[] = "Microsoft";
?>

Notice that you’re pushing these brands without assigning indices to any of them. When you do this, the values will be stored in indices in the order of the push (“Apple” will be assigned an index of 0, “Google” will be assigned 1, and “Microsoft” will be assigned 2).

If you explicitly wanted to state the index value for each, you would do this:

<?php
// Instantiate an empty array
$brands = [];

// Push the values to indices 0, 1, and 2
$brands[0] = "Apple";
$brands[1] = "Google";
$brands[2] = "Microsoft";
?>

Unsetting Values From the Array

Let’s say you made a mistake: you forgot to add “Facebook” to the list of brands. For one reason or another, you want the values to be stored in alphabetical order in the array.

One way to correct this is to unset “Google” and “Microsoft” from indices 1 and 2, then set the values anew and correctly.

(We’re doing this just for the sake of our example. We can just as well push the values properly without unsetting the existing ones.)

Here’s how this would work:

<?php
// Instantiate an empty array
$brands = [];

// Push the values to indices 0, 1, and 2
$brands[0] = "Apple";
$brands[1] = "Google";
$brands[2] = "Microsoft";

// Unset the values at indices 1 and 2
unset($brands[1]);
unset($brands[2]);

//Push the right values to indices 1, 2, and 3
$brands[1] = "Facebook";
$brands[2] = "Google";
$brands[3] = "Microsoft";
?>

Overwriting Values in the Array

If you had to overwrite one value in your array with another, simply push the new value to the array, along with the index of the value that you want to overwrite with it:

<?php
// Instantiate an empty array
$brands = [];

// Push the values to indices 0, 1, and 2
$brands[0] = "Apple";
$brands[1] = "Google";
$brands[2] = "Microsoft";

// Overwrite "Google" with "Facebook"
$brands[1] = "Facebook";
?>

Resetting the Array

To reset the array to empty, simply call it with empty brackets or the array() language construct again:

<?php
// Instantiate an empty array
$brands = [];

// Push the values to indices 0, 1, and 2
$brands[0] = "Apple";
$brands[1] = "Google";
$brands[2] = "Microsoft";

// Reset array to empty
$brands = [];
?>

Unsetting the Array

If you no longer need to store these values in this array object, you could unset() the whole object. When called, the unset() variable-handling function destroys the object from the memory:

<?php
// Instantiate an empty array
$brands = [];

// Push the values to indices 0, 1, and 2
$brands[0] = "Apple";
$brands[1] = "Google";
$brands[2] = "Microsoft";

// Unset (destroy) array
unset($brands);
?>

Conclusion

Thank you for reading this far and, for those of you who are just getting started with PHP, I hope this guide helped you learn not only how to instantiate an empty array, but how to use it.

If you have questions or tips to share with the rest of this post’s readers, leave a comment below.

Image courtesy of Przemyslaw Koch /Depositphotos

Leave a comment

Your email address will not be published. Required fields are marked *