What will be the output of following script?
PHP Code:
$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;
?>
What will be the output of following script?
PHP Code:
$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;
?>
Last edited by keyboard; 10-07-2013 at 11:45 AM. Reason: Format: Php Tags [php][/php]
Run it and see. My guess is an error, unless PHP type converts the extra array that's made here:
back into a number.$sum += $array[$array[$i]];
- John________________________
Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate
Program Output:
78
Out of curiosity, is there any reason you can't run this script on your own? (read below)
Edit -
Right - derp
Last edited by keyboard; 10-07-2013 at 12:24 PM.
Oh, it could be a trick question though. Look at the code in the original post, it has no opening <? or <?php in it. If run like that, it will output:
$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;
?>
As this code has now been run and the answer is definitive, I'm closing this thread.
And looking at it again, I see that there was never any need for type conversion. it's array[array[$i]], not array(array[$i]).
In the future, please don't post questions you can easily answer by simply running the code.
If you have nowhere to run the code, you shouldn't really be participating in this section of the forum.
If you need help setting up a local PHP environment for testing, open a new thread on that, and we will be happy to help you.
Last edited by jscheuer1; 10-07-2013 at 12:19 PM. Reason: saw "trick", and lack of type conversion
- John________________________
Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate
Bookmarks