View Full Version : PHP Program
mohamedismail
10-07-2013, 09:53 AM
What will be the output of following script?
$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;
?>
jscheuer1
10-07-2013, 11:45 AM
Run it and see. My guess is an error, unless PHP type converts the extra array that's made here:
$sum += $array[$array[$i]];
back into a number.
keyboard
10-07-2013, 11:47 AM
Program Output:
78
Out of curiosity, is there any reason you can't run this script on your own? (read below)
Edit -
Right - derp
jscheuer1
10-07-2013, 12:02 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.
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.