What is an array?
An array is simply a special variable which can hold many values. These values can be of same data types or different data types. PHP adjusts accordingly.
The basic syntax of an array:
$any_variable_name = array("thing", "something", "another_thing");
echo "Hello! Here's a " . $any_variable_name[0] . ", " . $any_variable_name[1] . " and " . $any_variable_name[2] . ".";
So, In the above example you can see that I have defined a variable named "any_variable_name" with a '$' sign, this is how we declare variables in PHP. Then I used array. In this array block array("thing", "something", "another_thing");
first I defined an array, then I began to fill content in the array that is: thing , something , another_thing . These three are basically contents that fill up this array and at the end ; to end the statement. This can also be called as a declaration statement as I am declaring an array.
Now about the second statement, I started with echo which is a print command or it displays output on the screen. Then a simple string output "Hello! Here's a " then I used a string concatination . , this is simply used to connect two strings together. Then there is this $any_variable_name[0]
. Now you might wonder what the hell!? but don't worry I got you covered.
There are basically two parts in this, one is simply variable name and another is the index number. Here you will notice that the variable name is the same name as I defined earlier. Then as there is an array stored in the variable we have to specify the index no corresponding to the value we want to display. In this example I wrote [0]
. This 0 corresponds to the first element in the array that is "thing". So $any_variable_name[0]
simply means "thing" and $any_variable_name[1]
means "something" and similarly $any_variable_name[2]
means "another_thing". At last ; to end the statement.
Summary
So, all we have done is just
- declare a variable
- stored an array into that variable
- printed all the values of array using echo statement
More helpful resources can be found on W3Schools.com. They seem to have easy to learn tutorials for web languages like HTML, CSS, PHP, JavaScript to name a few.
Happy Coding!