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 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 "; } 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; } } 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. 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 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. 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. Link to comment https://forums.phpfreaks.com/topic/230947-foreach-question/#findComment-1189173 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.