php PHP在while循环中添加+1

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9277336/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 06:28:58  来源:igfitidea点击:

PHP add +1 in while loop

phpwhile-loop

提问by harisdev

I have while loop and in that there's forum posts.I want to show number of posts (1,2..).

我有 while 循环,其中有论坛帖子。我想显示帖子数(1,2 ..)。

Let me show you what i think

让我告诉你我的想法

while($some = mysql_fetch_array($forum_posts)){
echo 'Number of post is $num++';
}

and show like

并显示像

------ Thread -------

- - - 线 - - - -

------ Posts -------

------ 帖子 -------

Text of post            1.
Text of post            2.
Text of post            3.

Thanks.Sorry for bad english

谢谢。抱歉英语不好

回答by core1024

$num must be initialized!

$num 必须初始化!

$num = 1;
while($some = mysql_fetch_array($forum_posts)){
echo 'Number of post is '.($num++);
}

回答by Vytautas

$num = 0;

while($some = mysql_fetch_array($forum_posts)){
    echo 'Number of post is '.++$num;
}

回答by hsz

Try with:

尝试:

$num = 1;
while($some = mysql_fetch_array($forum_posts)){
   echo 'Number of post is ' . ($num++) . '.';
}

Variables have to be outside string.

变量必须在字符串之外。