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
Timothy Boland
18,237 PointsThe accepted answer to the following question in the build-a-simple-php-application/listing-inventory-items/arrays Quiz is incorrect
The Question is: The following block of code displays the number of elements in an array named $flavors. What is that number?
$flavors = array("Vanilla", "Chocolate", "Strawberry");
$flavors[] = "Cookie Dough";
echo count($flavors);
The accepted answer is 1....but the correct answer should be 4....i ran the code myself
1 Answer
Timothy Boland
18,237 Pointsahhh....nevermind...i see the difference now
$flavors[] = "Cookie Dough";
adds a value to the existing array called $flavors
$flavors = array("Cookie Dough");
creates a "new" array with a single value of "Cookie Dough"...therefore the count equals 1
Christer Nordbø
2,133 PointsChrister Nordbø
2,133 PointsUnless there is something wrong with the code in your first post - i would actually agree with you. That code looks to me like it should return count = 4.
The difference like you explain it is correct - but as you can see, it creates the array with 3 items first, and then add extra item to it.
Timothy Boland
18,237 PointsTimothy Boland
18,237 PointsYes, the code in the first post is wrong...there are 2 almost identical questions in the quiz....specifying the difference between $flavors[] = "element"; and $flavors = array(element);
I would delete the post if I could, but that doesn't appear to be an option