PHP在while循环中添加+1

时间:2012-02-14 13:04:12

标签: php while-loop

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

让我告诉你我的想法

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

并显示为

------线程-------

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

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

谢谢。不好的英语

3 个答案:

答案 0 :(得分:10)

$ num必须初始化!

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

答案 1 :(得分:2)

$num = 0;

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

答案 2 :(得分:0)

尝试:

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

变量必须在字符串之外。