if else 函数问题?

发布于 2024-11-28 06:27:40 字数 923 浏览 1 评论 0原文

我在 while 循环中有以下带有 if...else 语句的代码。

$colour = array("50305A", "E33600", "DBDD02", "73BE58");
$nextcolr = next($colour);
 if ($nextcolr == FALSE)
 {
    reset($colour);
    echo current($colour);
 }
 else
 {
    echo next($colour);
 }

我无法弄清楚为什么 else 语句中的内容没有被执行,即使我切换这两个语句并反转运算符。有人可以帮助我吗?

整个 while 循环:

while($row = mysql_fetch_array($result))
  {
  echo "by <a href='/neuro/profile.php?userid=$row[MemberID]'>" . $row['FirstName'] . " " . $row['LastName'] . "</a> on " . $row['Timestamp'] . " | " . $row['NumberOfComments'] . " comments.";
  echo "<div id='blog' style='background-color:#";
  if ($nextcolr == FALSE)
    {
    reset($colour);
    echo current($colour);
    }
  else
    {
    echo next($colour);
    }
  echo "'><a href='blog.php?threadid=" . $row['tID'] . "'>" . $row['Title'] . "</a></div>";
  }

I have the following code with the if...else statement within a while loop.

$colour = array("50305A", "E33600", "DBDD02", "73BE58");
$nextcolr = next($colour);
 if ($nextcolr == FALSE)
 {
    reset($colour);
    echo current($colour);
 }
 else
 {
    echo next($colour);
 }

I can't work out why what ever is in the else statement isn't being executed, even if I switch the two statements and reverse the operator. Could anyone help me?

The entire while loop:

while($row = mysql_fetch_array($result))
  {
  echo "by <a href='/neuro/profile.php?userid=$row[MemberID]'>" . $row['FirstName'] . " " . $row['LastName'] . "</a> on " . $row['Timestamp'] . " | " . $row['NumberOfComments'] . " comments.";
  echo "<div id='blog' style='background-color:#";
  if ($nextcolr == FALSE)
    {
    reset($colour);
    echo current($colour);
    }
  else
    {
    echo next($colour);
    }
  echo "'><a href='blog.php?threadid=" . $row['tID'] . "'>" . $row['Title'] . "</a></div>";
  }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

情深已缘浅 2024-12-05 06:27:40
$colour = array("50305A", "E33600", "DBDD02", "73BE58");
while ... {
    $nextcolr = next($colour);
    if ($nextcolr === FALSE)
    {
        reset($colour);
    }
    echo current($colour);
}

这就是你的 while 循环应该是什么样子的。如果我是对的,您还在 while 循环中定义 $color,这可能会导致问题。

$colour = array("50305A", "E33600", "DBDD02", "73BE58");
while ... {
    $nextcolr = next($colour);
    if ($nextcolr === FALSE)
    {
        reset($colour);
    }
    echo current($colour);
}

is how your while loop should look like. If I am right, you are also defining $colour in the while loop, which might cause problems.

花开浅夏 2024-12-05 06:27:40

如果所有这些都在 while 循环中,那么您将在每次迭代时重新声明数组,从而将数组内部指针返回到每次迭代的开头。

If all this is in the while loop, then you are re-declaring the array on each iteration, thus returning the array internal pointer to the beginning with each iteration.

尝蛊 2024-12-05 06:27:40

如果你想多次迭代这个数组,你可以这样做:

 $colour = array("50305A", "E33600", "DBDD02", "73BE58");
 $i = 0;

 while ... {
    ...
    echo $colour[$i++ % count($colour)];
    ...
 }

所以你不需要这个 if-else 块。

If you want to iterate this array multiple times, you could do it this way:

 $colour = array("50305A", "E33600", "DBDD02", "73BE58");
 $i = 0;

 while ... {
    ...
    echo $colour[$i++ % count($colour)];
    ...
 }

So you don't need this if-else block.

沫离伤花 2024-12-05 06:27:40

原始 while 循环的问题是您从未更改 $nextcolr 的值。
因此,它始终保持 FALSE 并且 else 部分永远不会被执行。

The problem with your original while loop is that you never change the value of $nextcolr.
Thus, it always remains FALSE and the else part never gets executed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文