Skip to content

PHP Tutorial Data Types

Arrays

Lesson 39 of 369 2 min read By Deepak Paswan Updated Oct 5, 2026

An array holds many values in a single variable. Instead of $color1, $color2 and $color3, you keep one list.

index.php
PHP
<?php
$colors = ["red", "green", "blue"];

echo $colors[0] . "\n";
echo $colors[2];
Output
OUTPUT
red
blue

Positions start at 0, so the first item is [0].

Arrays with names

An associative array uses names (keys) instead of numbers.

index.php
PHP
<?php
$user = [
    "name" => "Isha",
    "age" => 31,
    "city" => "Indore",
];

echo $user["name"] . " lives in " . $user["city"];
Output
OUTPUT
Isha lives in Indore

Looking inside an array

Use print_r() for a quick, readable look.

index.php
PHP
<?php
$tags = ["php", "web"];
print_r($tags);
Output
OUTPUT
Array
(
    [0] => php
    [1] => web
)

var_dump() shows more detail (types and sizes). Both are for debugging, not for the visitor.

How many items?

index.php
PHP
<?php
echo count(["a", "b", "c"]);
Output
OUTPUT
3

Arrays can hold anything

Items can be of different types, and an array can even contain other arrays. Multidimensional Arrays covers that.

info
This is only the start
Arrays are the most used data structure in PHP. Chapter 11 is dedicated to them, with sorting, filtering and mapping.