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.

PHP Build a Basic PHP Website (2018) Listing and Sorting Inventory Items Working with Functions

Ben Os
Ben Os
20,008 Points

Couldn't decipher the logic of this php code

Hello, I am very new to programming and PHP. Here is a PHP code I admit I didn't manage to decipher. This question actually has 2 other twin-questions with 90% the same code in them - All 3 where very much un-undestandable to me even though I answered all others successfully; I am surprised Alena Holligan used such complicated-tricky code in such an early stage and actually 2 veteran php programmers I showed them the code where indeed surprised it is given to me in that course (I myself have knowledge in Psychology and education and I do feel, in contrast to all other learning materials I had so far, that this is a bit exaggerated). Anyway:

I do understand there are 6 variables in this code and a foreach internal loop that runs through the array and includes an if-then statement. I also understand that one of the 6 variables is situated in the foreach loop (and represents each array value independently) but I didn't understand the logic of the code in general.

Can someone write a pseudocode for this in a stepped way and detail as much as possible in a didactic way for newcomers like me? I know this code is not minimalistic and might seem unusual - I found it in a practice sheet.

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

?>

1 Answer

Mike Costa
PLUS
Mike Costa
Courses Plus Student 26,361 Points

Although I haven't watched this course yet, breaking this code down is easy:

<?php
// an array with 4 elements in it
$numbers = array(1,2,3,4);

// count() function gets how many elements in the array
$total = count($numbers); // 4

// self explanatory
$sum = 0;

// self explanatory, yet not used at all in this code
$output = "";

// used for an iterator 
$i = 0;

// this will loop through each element in the array
// the $numbers array can also be written like
// $numbers = array(0=> 1, 1 => 2, 2 => 3, 3=> 4);
// When you use a foreach loop like this, you're passing in the array $numbers
// and using a placeholder ($number) to represent the value of the element this 
// foreach loop iterates through from beginning element (0) to end element (3).
// I'll run through this loop step by step.

// Step 0. Enter loop
// Step 1. $number is first element of the $numbers array.  $number = 1.
// Step 2. $i = 0
// Step 3. Add 1 to $i
// Step 4. $i = 1
// Step 5. Check if $i is less than $total. (in this case, 1 is less than 4, so enter inside the if statement)
// Step 6. Add this number to $sum. $sum = 0 so $sum now equal 0 + 1)
// Step 7. $sum = 1.
// Step 8. Start back to the top of the foreach loop for the second element.
// Step 9. $number is second element of the $numbers array.  $number = 2
// Step 10. $i = 1
// Step 11. Add 1 to $i
// Step 12. $i = 2
// Step 13. Check if $i is less than $total. (in this case, 2 is less than 4, so enter inside the if statement)
// Step 14. Add this number to $sum. $sum = 1 so $sum now equal 1 + 2)
// Step 15. $sum = 3.
// Step 16. Start back to the top of the foreach loop for the third element.
// Step 17. $number is third element of the $numbers array.  $number = 3
// Step 18. $i = 2
// Step 19. Add 1 to $i
// Step 20. $i = 3
// Step 21. Check if $i is less than $total. (in this case, 3 is less than 4, so enter inside the if statement)
// Step 22. Add this number to $sum. $sum = 3 so $sum now equal 3 + 3)
// Step 23. $sum = 6.
// Step 24. Start back to the top of the foreach loop for the fourth and last element.
// Step 24. $number is last element of the $numbers array.  $number = 4
// Step 25. $i = 3
// Step 26. Add 1 to $i
// Step 27. $i = 4
// Step 28. Check if $i is less than $total. (in this case, 4 is NOT less than 4, so skip if statement)
// Step 29. exit loop, not more elements to iterate through
// Step 30, echo $sum (which equals 6)

foreach($numbers as $number) {
    $i = $i + 1;  

    if ($i < $total) {
        $sum = $sum + $number;
    }
}

echo $sum;

?>

If I had to guess, the reason the loop was written like this was to omit adding the last element of the array to the $sum. I hope this sheds some light on the flow of the foreach loop. If you every want to understand whats going on inside of a foreach loop and check which values are what, you could put a couple echo statements inside of it like so to see what's really happening.

foreach($numbers as $number) {
echo "Number = " . $number . "<br>";
    $i = $i + 1;  
echo '$i = ' . $i . "<br>";
    if ($i < $total) {
echo "Inside if statement <br>";
echo "Sum before: " . $sum . "<br>";
        $sum = $sum + $number;
echo "Sum after: " . $sum . "<br>";
    }
}

echo $sum;