serbestgezer Posted March 17, 2011 Share Posted March 17, 2011 Hi There, is there any way to not print last <br> on screen with foreach. $letters = array('a','b','c'); foreach($letters as $letter) { echo "$letter <br />"; } as html output a<br /> b<br /> c Quote Link to comment https://forums.phpfreaks.com/topic/230947-foreach-question/ Share on other sites More sharing options...
Maq Posted March 17, 2011 Share Posted March 17, 2011 Do a simple check: $letters = array('a','b','c'); foreach($letters as $letter) { echo (end($letters)==$letter) ? $letter : "$letter "; } Quote Link to comment https://forums.phpfreaks.com/topic/230947-foreach-question/#findComment-1188815 Share on other sites More sharing options...
litebearer Posted March 17, 2011 Share Posted March 17, 2011 I like Maqs' better, but... $letters = array('a','b','c'); $i=0; foreach($letters as $letter) { if($i==0){ echo $letter; $i=1; }else{ echo "<br/>" . $letter; } } Quote Link to comment https://forums.phpfreaks.com/topic/230947-foreach-question/#findComment-1188817 Share on other sites More sharing options...
serbestgezer Posted March 17, 2011 Author Share Posted March 17, 2011 cheers mate. Quote Link to comment https://forums.phpfreaks.com/topic/230947-foreach-question/#findComment-1188818 Share on other sites More sharing options...
kenrbnsn Posted March 17, 2011 Share Posted March 17, 2011 You don't have to use a foreach loop at all, just use the implode() function: <?php $letters = array('a','b','c'); echo implode('<br />',$letters); ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/230947-foreach-question/#findComment-1188832 Share on other sites More sharing options...
Maq Posted March 17, 2011 Share Posted March 17, 2011 A foreach will give you a bit more flexibility, just something to think about. Quote Link to comment https://forums.phpfreaks.com/topic/230947-foreach-question/#findComment-1188838 Share on other sites More sharing options...
serbestgezer Posted March 18, 2011 Author Share Posted March 18, 2011 A foreach will give you a bit more flexibility, just something to think about. thanks Ken, implode was more handy for my script. Quote Link to comment https://forums.phpfreaks.com/topic/230947-foreach-question/#findComment-1189173 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.