## Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

### Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today. # How is this equaling 6?

Hello everyone,

In a test I came across a question that asks, "what does the following code output?"

<?php

\$numbers = array (1 , 2 , 3 , 4 );

\$total = count ( \$numbers );

\$sum = 0;

\$output = "";

foreach( \$numbers as \$number ) {

```\$i = \$i + 1;

if  ( \$i < \$total ) {

\$sum = \$sum + \$number;

}
```

}

\$i = 0;

echo \$sum;

?>

Could anyone explain to me how this is getting the equaling 6? Thanks so much in advance for any info. This has got me stumped.

1 + 2 + 3 = 6

```// move \$i = \$i + 1; to below your for loop
//---or
// change if  ( \$i < \$total ) { to

if  ( \$i <= \$total ) {
``` Hi Kenneth,

There are a few problems with your code.

First, you need to initialize \$i before you start checking its value.

Second, the if statement is stopping before you go through it the 4th time, because \$total is equal to 4. You could change your < to <=.

However, I would probably drop the \$i and if statement altogether. A foreach loop will do this incrementing for you. Here's what I'd write:

```<?php

\$numbers = array (1 , 2 , 3 , 4 );

\$sum = 0;

foreach( \$numbers as \$number ) {
\$sum = \$sum + \$number;
}

echo \$sum;

?>
```

Hope this helps!

Greg