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

Understanding This Code

I had this code in the PHP quiz:

$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;

Can someone explain this to me? Why does it echo 6? I just dont know where that value has came from. Thanks any help would be great I just want to understand it a little better :)

2 Answers

Ok, originally $total = 4 (the number of elements in the array), $sum = 0, and $i = 0. Then you execute a loop 4 times - 1 for each element of the array:

  1. When loop executes first time, $number = 1 (the first element in the array). Then you add 1 to $i, and as a result $i = 0 + 1 = 1. As $i (1) is less than $total (4), you add $number (1) to the sum, and now $sum = 1;
  2. $number = 2 (the second element in the array). $i = 2. As $i (2) is less than $total (4), you add $number (2) to the sum, and now $sum = 3;
  3. $number = 3 (the third element in the array). $i = 3. As $i (3) is less than $total (4), you add $number (3) to the sum, and now $sum = 6;
  4. $number = 4 (4th element in the array). $i = 4. Then you check condition: now $i (4) is not less than $total (4), so you don't execute code inside the condition. Also, you reached last element in the array, so you exit the loop.

Now you output $sum, which was equal to 6 after the last change.

Here's a dry run of the program.

  1. $total becomes 4 because there are 4 elements in the $numbers array (The elements are 1, 2, 3 and 4)
  2. $i is set to 0; it is the string index that will be used to loop through the elements
  3. $i increments to 1. $i is less than 4 ($total), so the IF statement is entered
  4. $sum is 0. $number at this point is equal to 1, so now, the sum is 0 + 1 = 1
  5. $i increments to 2. $i is less than 4, so the IF statement is entered
  6. $sum is 1. $number at this point is equal to 2, so now the sum is 1 + 2 = 3
  7. $i increments to 3. $i is less than 4, so the IF statement is entered
  8. $sum is 3. $number at this point is equal to 3, so now the sum is 3 + 3 = 6
  9. $i increments to 4. $i is EQUAL to 4, so the IF statement is NOT entered and the foreach loop finishes since that was the last element. 4 is NOT added to the sum
  10. The final output is sum, which is 6

Dry runs, while cumbersome, will always help you understand a program, or even catch out logical errors in programs.