What Are Arrays?
An array in PHP lets you store multiple values in a single variable. Instead of creating a separate variable for every item, you can keep related data together in one place. This makes your code easier to read, organize, and update.
Arrays are useful when you need to work with lists of values such as names, numbers, products, tags, or settings. They are one of the most important building blocks in PHP because they help you manage data efficiently.
Why Arrays Matter
Without arrays, storing several related values would require many separate variables. That quickly becomes difficult to manage. Arrays solve this by grouping values together so you can loop through them, access individual items, and pass them into functions more easily.
- Keep related data together
- Reduce repetitive variables
- Make loops more useful
- Help organize program logic
- Prepare you for working with forms, databases, and APIs
Indexed Arrays
An indexed array uses numeric positions starting at 0. The first item is at index 0, the second item is at index 1, and so on.
$colors = [“red”, “blue”, “green”];
echo $colors[0];
This prints: red
In this example, the array stores three color values. When you use $colors[0], PHP returns the first item in the array.
Associative Arrays
An associative array uses named keys instead of numeric positions. This is helpful when each value has a label.
$user = [“name” => “Alex”, “role” => “Student”, “level” => “Beginner”];
echo $user[“name”];
This prints: Alex
Associative arrays are great when the data has meaning beyond its position. They are commonly used for user information, settings, and structured records.
Adding Items to an Array
You can add a new item to the end of an indexed array using empty square brackets.
$languages = [“PHP”, “JavaScript”];
$languages[] = “Python”;
print_r($languages);
After this runs, the array contains three values. PHP automatically places the new item at the next available index.
Looping Through Arrays
Arrays become especially powerful when combined with loops. A foreach loop is one of the easiest ways to go through every item in an array.
$fruits = [“apple”, “banana”, “orange”];
foreach ($fruits as $fruit) {
echo $fruit . “<br>”;
}
This prints each fruit on its own line. The loop takes one item at a time from the array and stores it in the $fruit variable while the loop runs.
Useful Array Functions
PHP includes many built-in functions for working with arrays. Here are a few beginner-friendly examples:
count()tells you how many items are in an arrayprint_r()shows the full contents of an arrayarray_push()adds one or more items to the endsort()sorts values in ascending order
$numbers = [3, 1, 2];
sort($numbers);
print_r($numbers);
After sorting, the array becomes [1, 2, 3]. These built-in tools save time and make array handling much easier.
Common Beginner Mistakes
- Forgetting that array indexes start at
0 - Trying to access an item that does not exist
- Mixing up indexed arrays and associative arrays
- Using the wrong key name in an associative array
As you practice, these mistakes become easier to spot. Reading your code carefully and testing small examples will help you understand how arrays behave.
Final Thoughts
Arrays are essential in PHP because they let you store and manage multiple values in a clear, flexible way. Once you understand indexed arrays, associative arrays, and looping through them, you will be ready for more advanced topics like multidimensional arrays, form handling, and database results.
If you are learning PHP step by step, arrays are a concept worth practicing often. They appear everywhere in real-world code and form the foundation for many other programming tasks.