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 triallewis tibbott
1,754 PointsNot the same output
<?php
function hello($arr) {
if (is_array($arr)) {
foreach ($arr as $name){
echo "Hello $name <br>";
}
} else {
echo "Hello";
}
}
$names = array("name1, name2, name3"); hello($names); ?>
When I run my code the output is hello name1 name2 name3 when it should be hello name1 hello name2 hello name3
I don't understand why its working like this, could someone please explain?
1 Answer
Riku S.
11,322 PointsThe array items are strings and each one of them has to wrapped inside quotes, now you have all of them wrapped in one pair of quotes.
// Your code
$names = array("name1, name2, name3");
// Change it to this
$names = array("name1", "name2", "name3");