## Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

### Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

# PHP foreach

\$numbers = array(1,2,3,4); \$total = count(\$numbers); \$sum = 0; \$loop = 0;

foreach(\$numbers as \$number) { \$loop = \$loop + 1; if (\$loop < \$total) { \$sum = \$sum + \$number; } }

echo \$sum;

I just took the quiz right here, I think the result should be 10, but why the result is 6?

Hope someone can tell me the detail, thank you so much!

in your pass: //first pass, loop = 1, if(1 < 4){sum = 0 + 1}, sum = 1 //second pass loop = 2, if(2 < 4) {sum = 2+2;}, sum = 3 //third pass loop = 3, if(3 < 4), {sum = 3+3;}, sum = 6 //fourth pass loop = 4, if(4 < 4), false, sum still equals 6

if you were to change to if (\$loop <= \$total), then the fourth pass would be true and your sum would equal 10.

//revised fourth pass loop = 4, if(4 <= 4), {sum = 6+4;}, sum = 10

Thank you for your help. So, what if there is no \$loop variable at here, then there is only one value will pass to the \$sum variable?