Question:What will the following script output?<?php
$array = array (1, 2, 3, 5, 8, 13, 21, 34, 55);
$sum = 0;
for ($i = 0; $i < 5; $i++) {
$sum += $array[$array[$i]];
}
echo $sum;
?>
+ ExplanationThis question is designed to test your ability to analyze a complex script more than your understanding of arrays. You may think it too convoluted—but we’ve all been faced with the not-so-pleasant task of debugging someone else’s code, and compared to some of the scripts we’ve seen, this is actually quite simple. The script simply cycles through the for loop five times, each time adding to $sum the value of the element of $array whose key is equal to the value of the element of $array whose key is equal to $i. It might sound a bit like a high-tech variation of “how much wood would a wood chuck chuck,” but if you step through the code manually, you’ll find that, when $i is zero, then $array[$array[$i]] becomes $array[$array[0]], or $array[1], that is, 2. Applied to all the iterations of the for loop, the resulting total is 78.