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

Help

Hi there, can somebody explain this quiz question? I dont understand it.

What does the following block of code output?

<?php

$flavors = array("Cake Batter","Cookie Dough");

foreach ($flavors as $a => $b) {

    echo $b;

    exit;

}
?>

answer:

  1. Cake BatterCookie Dough
  2. Cake Batter
  3. 0
  4. 01

I have no clue :-/ I dont understand the $b in this...

1 Answer

So array contain two things for each item, the index of the item and the value of the item. Sometimes these are called the key and the value. The key or index is the items position in the array and the value is whatever information that is stored at that position in the array.

In the example you proved above $a is a temporary variable that holds the index/key and $b is the temporary variable that holds the value. The foreach loop goes through each item in the given variable, in this case $flavors, and grabs the index and value of the current item in the array and then echoes that value to the screen.

Normally, if you wanted just the value of an item in an array you would right your foreach loop like this:

<?php 
    foreach($flavor as $value){
        echo $value;
    }
?>

The only time I would grad the index and the value is if i needed both. For example, say I had an array that held all of the information for a given user of a site:

<?php
    $user = array( 
        [name] => Andrew Shook
        [age]=> 28
        [Job]=> Web Design
    );
?>

Then if I wanted to print the index as a label and the information stored I would write:

<?php
    foreach($user as $label => $info){
        echo $label.": ".$info." ";
    }
?>

This would output:

 name: Andrew Shook age: 28 Job: Web designer

Thank you for your reply, I understand your example perfectly but the problem is that the array from the quiz looks like this:

$flavors = array("Cake Batter","Cookie Dough"); 

and not like:

$flavors = array (
[...] => "Cake Batter" 
[...] => "Cookie Dough" 
);

So I really don't understand what $b is in this quiz. And I got even more confused with the two last multiple choice answers I can choose from: 'C. 0' and 'D. 01'

I hope you have time to explain me the answer.

Thank you.

ps: and what would be the answer if the php code looked like this? (echo $a; instead of echo $b;)

<?php

$flavors = array("Cake Batter","Cookie Dough");

foreach ($flavors as $a => $b) {

echo $a;

exit;

}
?>

$b is the value of an array item at index/location $a of the array. So this array has two elements "Cake Batter" and "Cookie Dough". Because no location was specified for either, the first value entered into the array is at location zero and then the next value is at location one. I so in the example you gave above, the code would return "01", which are to locations in the array of its two elements.

By the way, to correct output for the original block of code would be "Cake BatterCookie Dough ".