An array is a special variable that can hold more than one value at a time. If you have a list of items (a list of car names, for example), storing the cars in single variables could look like this:
$car1 = "Volvo";
$car2 = "BMW";
$car3 = "Toyota";
However, what if you want to loop through the cars and find a specific one? What if you had 300 cars? The solution is an Array!
In PHP, arrays can be created using the array() function or the shorter square bracket [] syntax.
<?php
$cars = array("Volvo", "BMW", "Toyota");
// OR
$cars = ["Volvo", "BMW", "Toyota"];
?>
There are three main types of arrays that you will use frequently:
The count() function is used to return the number of elements currently stored in an array.
<?php
$cars = ["Volvo", "BMW", "Toyota"];
echo count($cars); // Outputs: 3
?>
$array[0].