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![](https://ecs-static.teamtreehouse.com/assets/views/marketing/shared/community-banner-white-47072046c51352fe6a69f5e691ff5700b28bb11d45197d7bdf066d9ea3f72d0c.webp)
![Amy Hsieh](https://uploads.teamtreehouse.com/production/profile-photos/10204502/micro_dog1.jpg)
Amy Hsieh
6,023 PointsI think I got the same code, but result is different
function hello($arr){ if (is_array($arr)){ foreach ($arr as $name){ } echo "Hello, $name <br>"; }else echo'Hello, Friends'; } $names=array ( "John","Mike","Charley");
hello($names);
My result : Hello Charley. It seems I have the same code as the example, but why the code can't print 'Hello John' and 'Hello Mike'?
1 Answer
![jamesjones21](https://uploads.teamtreehouse.com/production/profile-photos/9483372/micro_11813500_10156054945055144_3499832327951416355_n.jpg)
jamesjones21
9,260 PointsHi Amy
your setup should be like this:
function hello($arr){
if (is_array($arr)){
$names=array ( "John","Mike","Charley");
foreach ($arr as $name){
echo "Hello, $name <br>";
}
} else {
echo'Hello, Friends';
}
}
the echo within the foreach is outside of the foreach loop that's why the results contained nothing.
Damy Bosch
2,248 PointsDamy Bosch
2,248 PointsHello,
Your foreach is empty.
The echo "Hello, $name<br>" is outside your foreach