PHP CHAPTER 20

PHP Arrays

An array stores multiple values in one variable.

📘

What is it?

An array stores multiple values in one variable.

💡

Why do we use it?

Arrays are useful for lists of students, courses, products and other collections.

⚙️

How does it work?

Each value is stored under an index or key.

🧩

Syntax

PHP Syntax
$courses = ["PHP", "MySQL", "JavaScript"];
💻

Example

PHP Example
<?php

$courses = ["PHP", "MySQL", "JavaScript"];

echo $courses[0];

?>
🔍

Example Explained

The first element is stored at index 0, so $courses[0] returns PHP.
🚀

More Examples

<?php $courses = ["PHP", "MySQL", "JavaScript"]; foreach ($courses as $course) { echo $course . "<br>"; } ?>

⚠️ Important Rules

Indexed arrays normally start at index 0.

❌ Common Mistakes

Using an index that does not exist.

🌍 Real-World Use

Student lists, product catalogs and database results.

📝 Practice Exercise

Create an array of ten AIFA courses and display them.

💻 Try It Yourself

Write and run this PHP lesson inside the AIFA Coding Lab.

Open Coding Lab →