📖 Scenario: Imagine you are organizing a small library. You want to keep track of book titles so you can find them easily. Instead of creating a separate variable for each book, you use an array to store all the book titles together.
🎯 Goal: You will create an array to hold book titles, add a few titles, and then display them. This will show why arrays are essential in PHP for managing groups of related data.
📋 What You'll Learn
Create an array called books with three book titles
Add a new book title to the books array
Use a foreach loop with variable book to display each book title
Print each book title on a new line
💡 Why This Matters
🌍 Real World
Arrays help programmers manage lists of items like books, users, or products easily without creating many separate variables.
💼 Career
Understanding arrays is fundamental for any PHP developer because they are used in almost every PHP program to handle collections of data.
Progress0 / 4 steps
1
Create an array with book titles
Create an array called books with these exact book titles: 'The Hobbit', '1984', and 'Pride and Prejudice'.
PHP
Hint
Use square brackets [] to create the array and separate the titles with commas.
2
Add a new book title to the array
Add the book title 'To Kill a Mockingbird' to the books array using the [] syntax.
PHP
Hint
Use $books[] = 'To Kill a Mockingbird'; to add the new title at the end of the array.
3
Use a foreach loop to display book titles
Use a foreach loop with variable book to go through the books array and print each book title on its own line using echo.
PHP
Hint
Use foreach ($books as $book) { echo $book . "\n"; } to print each title on a new line.
4
Display the list of books
Run the program to display all book titles stored in the books array, each on a new line.
PHP
Hint
Make sure your program prints each book title on its own line exactly as shown.
Practice
(1/5)
1. Why are arrays essential in PHP?
easy
A. They can only store numbers, not text.
B. They replace functions.
C. They slow down the program.
D. They store multiple values in a single variable.
Solution
Step 1: Understand what arrays do in PHP
Arrays allow storing many values together in one variable, making data management easier.
Step 2: Evaluate the options
The statement 'They store multiple values in a single variable.' correctly describes arrays. The other statements are incorrect: arrays store any data types including text, do not slow down programs, and do not replace functions.
Final Answer:
They store multiple values in a single variable. -> Option D
Quick Check:
Arrays store many values = B [OK]
Hint: Arrays hold many values in one place [OK]
Common Mistakes:
Thinking arrays store only numbers
Confusing arrays with functions
Believing arrays slow down code
2. Which of the following is the correct way to create an array in PHP?
easy
A. $arr = array(1, 2, 3);
B. $arr = {1, 2, 3};
C. $arr = [1; 2; 3];
D. $arr = (1, 2, 3);
Solution
Step 1: Recall PHP array syntax
In PHP, arrays can be created using the array() function or square brackets []. The syntax array(1, 2, 3) is correct.
Step 2: Check each option
$arr = array(1, 2, 3); uses array() correctly. The other options use invalid syntax: curly braces {}, semicolons between elements, and parentheses without array keyword.
Final Answer:
$arr = array(1, 2, 3); -> Option A
Quick Check:
array() syntax = A [OK]
Hint: Use array() or [] to create arrays [OK]
Common Mistakes:
Using curly braces {} for arrays
Separating elements with semicolons
Using parentheses () without array keyword
3. What will be the output of the following PHP code?