Menu
Coddy logo textTech

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.

quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals