Previously we created several variables each one containing a single value.
We're a bit limited with this however.
For example let's say you want to display a list of articles you have several articles to display and
you have variables containing strings for the article titles like this.
But what if you don't know how many articles you're going to have are if you have a large number of
them you dont.
Now how many individual variables you need.
The solution is to use an array an array is simply a list of other values a single variable containing
an array can contain as few as many values as you want.
You can also decide how many values you have in an array when your code is running.
Not when you write it.
So this single variable here contains an array of three string values.
There are two ways to create an array in P HP.
You can use square brackets like this followed by the list of values separated by comers.
Alternatively you can use the array function like this by writing the word array again followed by the
list of values separated by comer's inside round brackets.
These two methods are equivalent.
You'll see both of these used in other people's P HP code.
Personally I prefer the square brackets method as I think it looks cleaner but it's up to you.
Let's create an array in our code.
This array has three elements each one being a string.
So now we have an array variable.
How do we use it.
We can't use echo as if we do that.
We get this error message.
If we use var dump instead.
It tells us that this variable contains an array with three values.
The first one is a 10 character string.
The second one is a 12 character string and so on.
Note that each value has been assigned a number starting from zero.
Each value in an array is traditionally referred to as an array element.
This array for example has three elements each element of the array has a unique value that identifies
it.
This is known as an index are key the elements index from 0.
So this is element 0.
This is Element 1 and this is element 2.
We can see this when we use var dump to display the contents of the variable.
You can access an individual that array element by putting the index in square brackets after the array
variable name like this.
You can treat one of these as though it was an individual variable.
So back in our code instead of the whole array let's just print out the value of the first element by
adding the index like that
in the browser.
It looks like this just like an individual variable.
We can do the same for the third element for example.
Don't forget the indexes start from zero.
So the third element has an index of two.
And in the browser This is what is printed out.
If we try and access an array elements with an invalid index
like that.
We get this error message.
We'll see later on how we can process every element of an array using a loop.