📖 Scenario: You are managing a small online store. You have a list of products in an array. You want to find out how many products are in the list.
🎯 Goal: Learn how to create an array, set a variable for counting, use the count() function to find the number of items in the array, and print the result.
📋 What You'll Learn
Create an array called products with these exact items: 'Apple', 'Banana', 'Orange', 'Mango'
Create a variable called totalProducts to store the count of items in the products array
Use the count() function with the products array to get the number of items
Print the value of totalProducts exactly as an integer
💡 Why This Matters
🌍 Real World
Counting items in a list is common in online stores, inventory systems, and any place where you manage collections of things.
💼 Career
Knowing how to count array items helps you handle data collections efficiently, a key skill for web developers and programmers.
Progress0 / 4 steps
1
Create the products array
Create an array called products with these exact items: 'Apple', 'Banana', 'Orange', 'Mango'
PHP
Hint
Use square brackets [] to create the array and separate items with commas.
2
Create the totalProducts variable
Create a variable called totalProducts and set it to 0 as a starting point
PHP
Hint
Use $totalProducts = 0; to create the variable.
3
Count the number of products
Use the count() function with the products array and assign the result to the variable totalProducts
PHP
Hint
Use count($products) to get the number of items in the array.
4
Print the total number of products
Write a print statement to display the value of totalProducts
PHP
Hint
Use print($totalProducts); to show the number.
Practice
(1/5)
1. What does the PHP function count() do when used on an array?
easy
A. It returns the number of elements in the array.
B. It returns the sum of all elements in the array.
C. It returns the last element of the array.
D. It sorts the array in ascending order.
Solution
Step 1: Understand the purpose of count()
The count() function in PHP is designed to return how many items are inside an array.
Step 2: Compare with other options
The other options describe summing elements, returning the last element, or sorting the array, which are unrelated to counting elements.
Final Answer:
It returns the number of elements in the array. -> Option A
Quick Check:
count() = number of elements [OK]
Hint: Remember: count() tells how many items, not their values. [OK]
Common Mistakes:
Thinking count() sums values instead of counting items
Confusing count() with functions that return elements
Assuming count() sorts or changes the array
2. Which of the following is the correct syntax to get the number of elements in an array $arr in PHP?
easy
A. length($arr);
B. sizeofArray($arr);
C. size($arr);
D. count($arr);
Solution
Step 1: Recall PHP function for array length
PHP uses count() to get the number of elements in an array.
Step 2: Check other options for validity
Functions like length(), size(), or sizeofArray() do not exist or are incorrect in PHP.
Final Answer:
count($arr); -> Option D
Quick Check:
Use count() for array size [OK]
Hint: Use count() to get array size, not length() or size(). [OK]
Common Mistakes:
Using length() which is not a PHP function
Confusing count() with size() or sizeofArray()
Adding semicolon inside function parentheses
3. What will be the output of the following PHP code?