Back to questions
JuniorTheoryCommon
What does the program print?
<?php
$prices = [10, 20, 30];
$total = 0;
foreach ($prices as $price) {
$price = $price * 2;
$total += $price;
}
echo $total, ' ', $prices[0], ' ', count($prices), "\n";120 10 3
- Expecting
$price = $price * 2to change the list:foreachputs a copy of each element into$price, so the list keeps10,20and30. - Expecting
countto return the last index,2: it returns how many elements the list holds, socount([10, 20, 30])is3.
- How would you change the loop so that it doubles the elements of
$pricesitself? - What does
$pricehold after the loop ends?