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 trialDonald Mak
1,603 PointsWhat's wrong with my code? Is that an html error?
I'm supposed to echo the string values stored under 'name' and 'email' keys of each of the arrays (0/1/2/3) under the array $contacts. I don't know why couldn't the values be shown? The system said problem was "array to string conversion" but I don't understand
<?php
//edit this array
$Alena=array('name'=>'Alena Holligan','email'=>'alena.holligan@teamtreehouse.com');
$Dave=array('name'=>'Dave McFarland','email'=>'dave.mcfarland@teamtreehouse.com');
$Treasure=array('name'=>'Treasure Porth','email'=>'treasure.porth@teamtreehouse.com');
$Andrew=array('name'=>'Andrew Chalkley','email'=>'andrew.chalkley@teamtreehouse.com');
$contacts = array($Alena, $Dave, $Treasure, $Andrew);
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";
1 Answer
Mike Bronner
16,395 PointsThis works:
<?php
$Alena=array('name'=>'Alena Holligan','email'=>'alena.holligan@teamtreehouse.com');
$Dave=array('name'=>'Dave McFarland','email'=>'dave.mcfarland@teamtreehouse.com');
$Treasure=array('name'=>'Treasure Porth','email'=>'treasure.porth@teamtreehouse.com');
$Andrew=array('name'=>'Andrew Chalkley','email'=>'andrew.chalkley@teamtreehouse.com');
$contacts = array($Alena, $Dave, $Treasure, $Andrew);
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";
?>
See this SO discussion for details: https://stackoverflow.com/questions/35598187/when-to-wrap-curly-braces-around-a-variable
Donald Mak
1,603 PointsDonald Mak
1,603 PointsGreat! Thanks Mike! Is adding { } something related to html or php syntax?
Mike Bronner
16,395 PointsMike Bronner
16,395 PointsIt's a PHP thing. I think it used to work in PHP 5 without them, and you might need them now in PHP7, but not 100% sure if that's the cause or not. I know that the curly braces are used for dynamic variable scoping in PHP 7, for example:
(see Uniform Variable Syntax: https://blog.digitalocean.com/getting-ready-for-php-7/)