What is the main purpose of array functions in PHP?
Array functions help you work with lists of data easily, like sorting, searching, or changing items without writing long code.
Click to reveal answer
beginner
How do array functions improve code readability?
They let you write simple, clear commands instead of complex loops, making your code easier to understand and maintain.
Click to reveal answer
intermediate
Name two common PHP array functions and their use.
array_map() applies a function to every item in an array; array_filter() removes items that don't meet a condition.
Click to reveal answer
intermediate
Why do array functions matter for performance?
They are optimized in PHP to run faster than manual loops, saving time when processing large data sets.
Click to reveal answer
beginner
How do array functions help avoid bugs?
Using built-in functions reduces mistakes because they are tested and reliable, unlike custom code that might have errors.
Click to reveal answer
Which PHP function applies a callback to each element of an array?
Aarray_merge()
Barray_filter()
Carray_map()
Darray_slice()
✗ Incorrect
array_map() runs a function on every item in the array.
What is a key benefit of using PHP array functions?
AThey improve code readability and performance
BThey make code longer and complex
CThey only work with strings
DThey replace the need for variables
✗ Incorrect
Array functions simplify code and often run faster than manual loops.
Which function removes elements from an array based on a condition?
Aarray_map()
Barray_pop()
Carray_push()
Darray_filter()
✗ Incorrect
array_filter() keeps only elements that meet the condition.
Why are built-in array functions less error-prone?
AThey are written by beginners
BThey are tested and optimized by PHP developers
CThey never change
DThey do not work with large arrays
✗ Incorrect
Built-in functions are reliable because they are well tested.
Which of these is NOT a reason why array functions matter?
AThey replace the need to learn PHP
BThey make code easier to read
CThey help process data efficiently
DThey reduce bugs in code
✗ Incorrect
Array functions do not replace learning PHP; they help you write better PHP code.
Explain why using PHP array functions is better than writing manual loops for handling arrays.
Think about how built-in tools save time and reduce mistakes.
You got /4 concepts.
List and describe two PHP array functions that help manipulate array data.
One applies a function to all items, the other filters items by condition.
You got /3 concepts.
Practice
(1/5)
1. Why are PHP array functions important when working with lists?
easy
A. They make PHP run faster by changing the language core.
B. They simplify tasks like sorting and searching in arrays.
C. They allow arrays to store only numbers.
D. They prevent errors by disabling user input.
Solution
Step 1: Understand the role of array functions
Array functions provide ready-made tools to handle common tasks like sorting, searching, and counting elements in arrays.
Step 2: Compare options
Only They simplify tasks like sorting and searching in arrays. correctly states that array functions simplify tasks with arrays. Other options describe unrelated or incorrect effects.
Final Answer:
They simplify tasks like sorting and searching in arrays. -> Option B
Quick Check:
Array functions simplify array tasks = A [OK]
Hint: Remember: array functions make list tasks easier [OK]
Common Mistakes:
Thinking array functions change PHP core speed
Believing arrays can only hold numbers
Confusing array functions with input validation
2. Which of the following is the correct syntax to count elements in an array $arr in PHP?
easy
A. length($arr);
B. size($arr);
C. count($arr);
D. countElements($arr);
Solution
Step 1: Recall PHP function for counting array elements
The correct function to count elements in an array is count().
Step 2: Check each option
Only count($arr); uses count($arr); which is valid PHP syntax. Others are not valid PHP functions.
Final Answer:
count($arr); -> Option C
Quick Check:
Counting array elements uses count() = B [OK]
Hint: Use count() to get array size quickly [OK]
Common Mistakes:
Using length() which is not a PHP function
Trying size() which doesn't exist in PHP
Assuming countElements() is built-in
3. What will be the output of the following PHP code?
The condition uses assignment '=' instead of comparison '==', so it always assigns true and evaluates as true.
Step 2: Correct the condition
It should be if ($found == true) or simply if ($found) to check the boolean value.
Final Answer:
Using '=' instead of '==' in the if condition -> Option D
Quick Check:
Use '==' to compare, not '=' [OK]
Hint: Use '==' for comparison, '=' is assignment [OK]
Common Mistakes:
Confusing '=' with '==' in conditions
Thinking in_array() is invalid
Missing semicolon after echo (actually present)
5. You have an array of user ages: $ages = [21, 17, 30, 15, 25];. Using PHP array functions, which code snippet correctly filters out ages less than 18 and returns the filtered array?
hard
A. $adults = array_filter($ages, fn($age) => $age >= 18);
B. $adults = array_map(fn($age) => $age >= 18, $ages);
D. $adults = array_search(fn($age) => $age >= 18, $ages);
Solution
Step 1: Understand the goal
We want to keep only ages 18 or older, so we need a function that filters elements based on a condition.
Step 2: Identify correct array function
array_filter() keeps elements where the callback returns true. $adults = array_filter($ages, fn($age) => $age >= 18); uses it correctly with a function checking age >= 18.
Step 3: Check other options
$adults = array_map(fn($age) => $age >= 18, $ages); uses array_map() which transforms elements but does not filter. $adults = array_reduce($ages, fn($carry, $age) => $age >= 18 ? $age : $carry); uses array_reduce() incorrectly for filtering. $adults = array_search(fn($age) => $age >= 18, $ages); uses array_search() wrongly with a callback.