Thuta Learning
BasicProgrammingbeginner

Arrays

Relax. We'll talk through this in plain words — no textbook voice.

An array is a structure that lets you group many values into a single variable. Arrays get heavy use in menu lists, product lists, user profiles, cart items, and the like. An indexed array uses number indexes, while an associative array uses named keys.

php
<?php
// Indexed array
$skills = ["HTML", "CSS", "PHP"];
echo $skills[2] . "<br>";

// Associative array
$user = [
  "name" => "Mya Mya",
  "role" => "Student"
];
echo $user["name"] . " is a " . $user["role"];
?>
You should see
PHP Mya Mya is a Student

Easy traps

  • Forgetting that array indexes start at 0 and calling $skills[3] can give you an undefined offset.
Arrays | Thuta Learning