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 Arrays Multidimensional Arrays

john paul
john paul
3,191 Points

cannot get task 3 to output

I got task 1- 3 correct , but I cannot understand why my code is task 3 wont output.

index.php
<?php
//edit this array
//$contacts = array('Alena Holligan', 'Dave McFarland', 'Treasure Porth', 'Andrew Chalkley');
$contacts [] = [
  'name' => 'Alena Holligan',
  'email' => 'alena.holligan@teamtreehouse.com',
  ];
$contacts [] = [
  'name' => 'Dave McFarland',
  'email' => 'dave.mcfarland@teamtreehouse.com',
  ];
$contacts [] = [
  'name' => 'Treasure Porth',
  'email' => 'treasure.porth@teamtreehouse.com',
  ];
$contacts [] = [
  'name' => 'Andrew Chalkley',
  'email' => 'andrew.chalkley@teamtreehouse.com',
  ];

echo "<ul>\n";
//$contacts[0] will return 'Alena Holligan' in our simple array of names.
echo "<li>$contacts[0]['name'] .': '. $contacts[0]['email']</li>\n";
echo "<li>$contacts[1]['name'] .': '. $contacts[1]['email']</li>\n";
echo "<li>$contacts[2]['name'] .': '. $contacts[2]['email']</li>\n";
echo "<li>$contacts[3]['name'] .': '. $contacts[3]['email']</li>\n";
echo "</ul>\n";
?>
/*
echo "<ul>\n";
//$contacts[0] will return 'Alena Holligan' in our simple array of names.
echo "<li>Alena Holligan : alena.holligan@teamtreehouse.com</li>\n";
echo "<li>Dave McFarland : dave.mcfarland@teamtreehouse.com</li>\n";
echo "<li>Treasure Porth : treasure.porth@teamtreehouse.com</li>\n";
echo "<li>Andrew Chalkley : andrew.chalkley@teamtreehouse.com</li>\n";
echo "</ul>\n";
*/

5 Answers

Marwa Rashad
Marwa Rashad
4,198 Points

Hello John, I am sorry for the late reply. I am glad you worked it out. Let me tell you more of what I understand. Without concatenation, PHP will understand that the echo statement is ended after the double quotation marks ("<li>"). So it will find a variable alone without any context which will cause an error. Remember that concatenation is used to merge more than one string into one and echo() expects one string to echo, or two strings separated by a comma. Please check more about echo() in the documentation http://php.net/manual/en/function.echo.php. I hope this makes it clearer.

john paul
john paul
3,191 Points

it does make more sense now, thank you !!

Marwa Rashad
Marwa Rashad
4,198 Points

Hi there! It’s obviously a concatenation error. You have used the double quotation marks and the “.” as well on concatenation. You have to separate those parts. I suggest you revise the concatenation rules and try again. Remember you can always use curly braces with variables in case you need to save yourself the hustle. Finally, you will always find someone who asked the same question before you. Make sure you check the questions on the video before the challenge or the one after it to gain more insight. Happy coding!

john paul
john paul
3,191 Points

Thanks Marwa, I will revisit concatentation. I did try and find if the question had been asked before. Obviously I did not try hard enough. Many thanks !!

Marwa Rashad
Marwa Rashad
4,198 Points

Always welcome! Good luck!

john paul
john paul
3,191 Points

Hi, there is one thing I still do not understand Marwa, why we have to concatenate the array name $contacts[] after "<li>"

i.e. echo "<li>" . $contacts[0]['name'], ' : ' . $contacts[0]['email'] . "</li>\n";

john paul
john paul
3,191 Points

Hi, it's ok,, I have worked it out... the code is only asking to print the tag name, there we need the double quotes and the concatenation.