PHP Arrays
PHPArrays store multiple values in a single variable. PHP has two types:
- Indexed arrays — numbered keys (0, 1, 2...)
- Associative arrays — named keys ("name", "age"...)
Arrays are the most used data structure in PHP. Master them!
Example — PHP
<?php
// Indexed array
$colors = ["Red", "Green", "Blue"];
echo $colors[0]; // Red
echo "<br>";
echo "Total colors: " . count($colors);
echo "<br><br>";
// Associative array
$student = [
"name" => "Priya",
"age" => 16,
"class" => "10th",
"marks" => 92
];
echo "Name: " . $student["name"] . "<br>";
echo "Class: " . $student["class"] . "<br>";
echo "Marks: " . $student["marks"] . "%";
?>
Result
Red
Total colors: 3
Name: Priya
Class: 10th
Marks: 92%
Total colors: 3
Name: Priya
Class: 10th
Marks: 92%