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 trialA X
12,842 PointsWhy is Implode in PHP Useful?
I'm not understanding the use(s) for implode in PHP when referencing an array. I get it that implode prints out the values of an array, but when would you ever need to do this & why? If someone could provide a simple example, I'd appreciate it.
2 Answers
Benjamin Payne
8,142 PointsImplode is useful when you just need to put the elements of an array in some delimited form. For example say you had an array with a City, State, and Zip code in it. Instead of looping through the array you can just use implode to get a comma separated string:
<?php
$address = ['123 Sand Street', 'Mos Eisley', '45891'];
echo implode(', ', $address);
// 123 Sand Street, Mos Eisley, 45891
Hope that helps.
Thanks,
Ben
kevin hudson
Courses Plus Student 11,987 PointsWOW...Five years and this seems like extra work compared to other programming languages. Feels like the developers that improved PHP were just trying to be different. I mean, in just about any other language you can access an array with console.log(array) static method now we have to be different and call it 'implode', and on top of that we have to specify the delimiter. There must be a better method like console.log or writeline. It's like taking a step back just to be different.
Julian Helmholz
3,242 PointsJulian Helmholz
3,242 PointsCorrect, but the delimiter should be first:
echo implode("\n", $learn);
Benjamin Payne
8,142 PointsBenjamin Payne
8,142 PointsWow...three years on and no one caught that. This has been updated.