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.

Start your free trial

PHP Build a Simple PHP Application Wrapping Up The Project Wrapping Up The Project

Benjamin Payne
Benjamin Payne
8,142 Points

I cannot understand why the answer is six? It is six...but why?

I have echoed out and var_dumped every possible variable in ever possible way to try and understand this one and for the life of me I cannot figure out why it echoes out '6' for $sum.

4 Answers

Andrew Moore
Andrew Moore
21,076 Points

Hi Benjamin, So this has to do with where the $i variable is incremented. At first glance, it seems as though the sum should be equal to 10 if we were actually adding all of the numbers in the array. However, because $i is incremented before testing it against the $total variable, we're not actually counting all of the numbers.

Taking each iteration step by step (Starting with $total = 4, $i = 0, $sum = 0):

  1. $i is incremented - so $i = 1

    $i is still less than 4, so the first value in the array(1) is added to the sum and $sum = 1

  2. $i is incremented - so $i = 2

    $i is still less than 4, so the second value in the array(2) is added to the sum and $sum = 3

  3. $i is incremented - so $i = 3

    $i is still less than 4, so the third value in the array(3) is added to the sum and $sum = 6

  4. $i is incremented - so $i = 4

    $i is no longer less than 4, so the last number in the array is never actually added to the sum and $sum is still 6

Hope this helps...

Hey Benjamin!

I assume you're talking about this question?

<?php

function sum_two_numbers($one,$two) {

    $sum = $one + $two;

    return $sum;

}

sum_two_numbers(1,5);

?>

The sum_two_numbers function takes 2 parameters representing 2 different numbers. In this case, $one contains an integer with the value 1 and $two contains an integer with the value 5. The function adds the two numbers, so 1 + 5, returning 6.

Benjamin Payne
Benjamin Payne
8,142 Points

Hey Aimee, I'm actually talking about a different one. The one you posted is a trick question because there is no 'echo' command ;) but I fully understand that the return value would be 6 on that. However, I do not understand why the return value is 6 on this one:

<?php

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

$total = count($numbers);

$sum = 0;

$output = "";

$i = 0;

foreach($numbers as $number) {

    $i = $i + 1;

    if ($i < $total) {

        $sum = $sum + $number;

    }

}

echo $sum;

?>

What is the value of $sum?

Benjamin Payne
Benjamin Payne
8,142 Points

Hi Andrew, That helps a ton. I edited your answer to what makes more sense to me. Does this still seem correct or am I not getting it still?

(Starting with $total = 4, $i = 0, $sum = 0): 1.

$i is incremented - so $i = 1 $i is still less than 4, so the first value in the array(1) is added to the sum where:

$sum = 0 + 1 (first-value in the array).

$i is incremented - so $i = 2 $i is still less than 4, so the second value in the array(2) is added to the sum where:

$sum = 1 + 2 (second-value in the array).

$i is incremented - so $i = 3 $i is still less than 4, so the third value in the array(3) is added to the sum where:

$sum = 3 + 3 (third-value in the array).

$i is incremented - so $i = 4 $i is no longer less than 4, so the last number in the array is never actually added to the sum and $sum is still 6

Andrew Moore
Andrew Moore
21,076 Points

Looks like you've got it.

So, if you were to modify the conditional and test if $i <= 4 instead, or moved the $i=$i+1 after the conditional, you'd get your expected sum of 10 because the last value in the array would be added to the sum.