justinh Posted February 3, 2009 Share Posted February 3, 2009 <?php $colors = array("red", "blue", "green"); $ct = count($colors); for($i = 0; $i <= $ct; ++$i){ $rainbow .= $colors[$i].","; } echo $rainbow; ?> this returns red,blue,green,, I'm trying to get it to return red,blue,green, Link to comment https://forums.phpfreaks.com/topic/143576-quick-question/ Share on other sites More sharing options...
justinh Posted February 3, 2009 Author Share Posted February 3, 2009 nvm fixed it myself <?php $colors = array("red", "blue", "green"); $ct = count($colors); for($i = 0; $i <= $ct; ++$i){ if($i == $ct){ $rainbow .= $colors[$i]; }else{ $rainbow .= $colors[$i].","; } } echo $rainbow; ?> Link to comment https://forums.phpfreaks.com/topic/143576-quick-question/#findComment-753355 Share on other sites More sharing options...
trq Posted February 3, 2009 Share Posted February 3, 2009 Better off using implode $colors = array("red", "blue", "green"); echo implode(',', $colors); Link to comment https://forums.phpfreaks.com/topic/143576-quick-question/#findComment-753387 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.