📖 Scenario: You are managing a small fruit shop. You have a list of fruits you sell, and you want to show each fruit's name to your customers.
🎯 Goal: Build a PHP script that uses a foreach loop to go through an array of fruits and print each fruit's name.
📋 What You'll Learn
Create an array called fruits with the exact values: 'Apple', 'Banana', 'Cherry', 'Date', 'Elderberry'
Create a variable called count and set it to 0
Use a foreach loop with the variable fruit to iterate over the fruits array
Inside the loop, increment count by 1
Inside the loop, print the current fruit's name on its own line
After the loop, print the total number of fruits using the count variable
💡 Why This Matters
🌍 Real World
Loops like foreach are used to process lists of items such as products, users, or messages in many real applications.
💼 Career
Understanding foreach loops is essential for PHP developers to handle arrays and collections efficiently in web development.
Progress0 / 4 steps
1
Create the fruits array
Create an array called fruits with these exact values: 'Apple', 'Banana', 'Cherry', 'Date', 'Elderberry'
PHP
Hint
Use square brackets [] to create the array and separate the fruit names with commas.
2
Create a counter variable
Create a variable called count and set it to 0
PHP
Hint
Use the = sign to assign the value 0 to the variable count.
3
Use a foreach loop to iterate over fruits
Use a foreach loop with the variable fruit to iterate over the fruits array. Inside the loop, increment count by 1 and print the current fruit's name on its own line.
PHP
Hint
Use foreach ($fruits as $fruit) to loop. Use $count++ to add 1. Use echo to print.
4
Print the total number of fruits
After the loop, print the total number of fruits using the count variable with the exact text: Total fruits: X where X is the value of count.
PHP
Hint
Use echo "Total fruits: $count\n"; to print the total count.
Practice
(1/5)
1. What does the foreach loop do in PHP when used with an array?
easy
A. It only visits the first item in the array.
B. It visits each item in the array one by one.
C. It deletes all items in the array.
D. It reverses the order of the array.
Solution
Step 1: Understand the purpose of foreach
The foreach loop is designed to go through each element in an array, one at a time.
Step 2: Compare options with foreach behavior
Only It visits each item in the array one by one. correctly describes visiting each item. Others describe actions foreach does not perform.
Final Answer:
It visits each item in the array one by one. -> Option B
Quick Check:
foreach loops visit all items [OK]
Hint: Remember: foreach loops through all array items [OK]
Common Mistakes:
Thinking foreach only visits first item
Confusing foreach with array modification functions
Assuming foreach changes array order
2. Which of the following is the correct syntax to loop through an array $colors using foreach to get each value?
easy
A. foreach ($colors in $color) { echo $color; }
B. foreach $colors as $color { echo $color; }
C. foreach ($colors as $color) { echo $color; }
D. foreach ($color as $colors) { echo $color; }
Solution
Step 1: Identify correct foreach syntax
The correct syntax is: foreach (array as value) { ... } with parentheses and 'as' keyword.
Step 2: Check each option
foreach ($colors as $color) { echo $color; } matches correct syntax. Options A, B, and D have syntax errors or wrong order.
Final Answer:
foreach ($colors as $color) { echo $color; } -> Option C
Quick Check:
Correct foreach syntax uses 'foreach (array as value)' [OK]
Hint: Use parentheses and 'as' keyword in foreach [OK]
foreach requires 'as' keyword between array and variable.
Step 2: Identify missing keyword
The code misses 'as' between $numbers and $num, causing syntax error.
Final Answer:
Missing 'as' keyword in foreach loop. -> Option A
Quick Check:
foreach needs 'as' keyword [OK]
Hint: Always include 'as' in foreach syntax [OK]
Common Mistakes:
Omitting 'as' keyword
Confusing variable and array order
Assuming variables need prior declaration
5. Given the array $items = ['a' => 1, 'b' => 0, 'c' => 3, 'd' => null];, which foreach loop will print only keys with truthy values?
hard
A. foreach ($items as $key => $value) { echo $key; }
B. foreach ($items as $key => $value) { if ($value === 0) echo $key; }
C. foreach ($items as $value) { if ($value) echo $value; }
D. foreach ($items as $key => $value) { if ($value) echo $key; }
Solution
Step 1: Understand truthy values in PHP
Values like 1 and 3 are truthy; 0 and null are falsy.
Step 2: Analyze each loop's condition
foreach ($items as $key => $value) { if ($value) echo $key; } prints keys only if value is truthy, matching requirement. Others print keys regardless or check wrong condition.
Final Answer:
foreach ($items as $key => $value) { if ($value) echo $key; } -> Option D
Quick Check:
Check value truthiness before echo key [OK]
Hint: Use if ($value) to filter truthy values [OK]
Common Mistakes:
Printing keys without condition
Checking for zero instead of truthy
Not using key => value syntax
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.