Introduction to Arrays
Part of the Fundamentals section of Coddy's PHP journey — lesson 34 of 71.
So far, you've worked with variables that hold a single value—one number, one string, one boolean. But what if you need to store a list of items, like a collection of usernames or a set of prices?
This is where arrays come in. An array is a special variable that can hold multiple values at once, organized as a list. Instead of creating separate variables like $fruit1, $fruit2, $fruit3, you can store them all in a single array.
<?php
$fruits = ["Apple", "Banana", "Cherry"];
?>Think of an array like a numbered list. Each item has a position (called an index) starting from 0. In the example above, "Apple" is at position 0, "Banana" at position 1, and "Cherry" at position 2.
Cheat sheet
An array is a variable that can hold multiple values organized as a list.
Create an array using square brackets:
$fruits = ["Apple", "Banana", "Cherry"];Each item in an array has an index (position) starting from 0:
- "Apple" is at index 0
- "Banana" is at index 1
- "Cherry" is at index 2
Try it yourself
This lesson doesn't include a code challenge.
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Comparison & Logical Operators
Comparison OperatorsEquality & IdentityLogical Operators Part 1Logical Operators Part 2Recap - Simple Logic2Variables and Data Types
NumbersStrings and QuotesBooleansNaming ConventionsRecap - Variable InitEmpty VariablesString ConcatenationGetting User InputCast to Different Types5Conditional Logic
If StatementIf - ElseThe Ternary OperatorNull Coalescing OperatorSwitch StatementRecap - Making Decisions3Basic Operators
Arithmetic OperatorsModulo OperatorExponentiation OperatorCombined AssignmentIncrement/DecrementOperator PrecedenceRecap - Simple CalculationsString Operators6Arrays Part 1 - Indexed
Introduction to ArraysCreating Indexed ArraysAccessing Elements by IndexModifying Elements by IndexArray Size with CountAdding Elements to an ArrayRecap - Managing a Simple List