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 PHP Arrays and Control Structures PHP Loops While Listing Array Values

So confused about the $count

//increment before evaluating the $count variable $count=0; while ((list($key, $val) = each($todo)) && (++$count <= 2)) { echo "$key => $val\n"; }

//increment after evaluating the $count variable $count=1; while ((list($key, $val) = each($todo)) && ($count++ <= 2)) { echo "$key => $val\n"; }

I get how the list and the while functions list out the values in the array but I really do not understand how creating a new variable of $count only displays 2 values in this loop. How does a variable affect this loop? I thought only functions could tell us what to do with information and variable store the information. I am so confused.

6 Answers

Michael Smith
Michael Smith
2,572 Points

Yeah, count starts at 0, then increments each time the while loop runs... when the count reaches 2.. the condition $count < 2 fails, and the while loops ends

A for loop would be better... They did it this way using a while loop because for loop hadn't been taught yet (I think!)

Okay thanks, Michael!

Edward Lee
Edward Lee
1,805 Points

I agree with Cliff. The PHP videos are clear until this very video. I've been re-watching and this video runs through a lot without really explaining.

I'm not sure how that code works, either. I think writing it like this is clearer:

$count = 0;
foreach ($learn as $key => $val) {

    if ($count < 2) {
         echo "$key $val\n";

    }

    $count++;
}
$count = 0;
while ((list($key, $val) = each($learn)) && $count++ < 2) {
    echo "$key => $val\n" . "<br>";
}

((list($key, $val) = each($learn)) ---> Gets the keys and the values inside the $learn array

&& $count++ < 2) ---> As long as the $count variable is less than 2...

echo "$key => $val\n" . "<br>"; ---> ...show the value along with its key from the $learn array

Each loop increments 1 to the variable $count. The code loops until $count is no longer less than 2.

the $count++ in the operation will add 1 during the first iteration. The next Iteration will add another 1 making it $count = 2. I understood this but man this is so different from other programming languages that use for loops or foreach. I have to adjust.

Michael Smith
Michael Smith
2,572 Points

I don't blame you! I'm far from a beginner coder, yet it took me a while to figure out what the hell count was doing... she's a very messy / lazy coder, which is fine and all... just not for an instructional video.

Did you ever figure out what it means and how it functions?

Cliff Jackson
Cliff Jackson
2,887 Points

This has become very confusing all of a sudden, the first time i have actually felt like walking away from this, i feel this should of been covered in far greater detail