Create a list using a PHP array

Sometimes you need to store a list of data. In this example of a blog, this may be a collection of tags to categorize the content of our posts.

First let’s remove this match statement and the message below, as we won’t be using this logic anymore.

Next let’s create a $tags variable, and we will set it equal to a left and right bracket. Within this bracket, can define any number of items, separated with commands. We can store a list of numbers, for example a list of years.

$tags = [1999, 2000, 2001];

We can also store a list of strings, and this is what we will do to store our list of tags. If we are creating a blog about programming, this may be php, docker, & mysql. We can store any number of tags in this array, and it can be as long as you wish.

$tags = ['php', 'mysql', 'docker'];

This is similar to creating three separate variables, but is more concise.

Complete and Continue  
Extra lesson content locked
Enroll to access source code, comments & track progress.
Enroll for Free to Unlock