如何在while循环中的最终数字后删除尾随逗号

试图删除数字 10 之后的逗号。尝试了所有可行的解决方法,但到目前为止没有任何效果。


$i = 0;


while($i < 10) {

echo ++$i. ",";

}


青春有我
浏览 132回答 2
2回答

www说

首先不要使用循环。用于implode()在数组元素之间插入分隔符。echo&nbsp;implode(',',&nbsp;range(1,&nbsp;10));

幕布斯6054654

你可以使用循环来做到这一点,rtrim()就像这样:$i = 0;$result = '';while($i < 10) {&nbsp; $result .= ++$i. ",";}echo rtrim($result, ',');
打开App,查看更多内容
随时随地看视频慕课网APP