Back to questions
JuniorTheoryCommon
What does the program print?
<?php
$prices = [10, 20, 30];
$withTax = array_map(fn($p) => $p * 1.1, $prices);
$total = array_reduce($prices, fn($sum, $p) => $sum + $p, 0);
echo count($withTax), ' ', $total, ' ', $prices[0], "\n";3 60 10
- Expecting
array_mapto change$prices: it returns a new array and leaves the original as it was, so$prices[0]is still10. - Expecting
$sumto start from0on every call:array_reducepasses what one call returns into the next one, starting from its third argument.
- What does
array_reducereturn for an empty array when no initial value is given? - How would you get the same total with one built-in function?