而其他语句?PHP
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/14951708/
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
while else statement? PHP
提问by Yinks
So I have a validation in null values using while statement the code is
所以我使用 while 语句对空值进行了验证,代码是
while (!$rs->EOF){ 
    echo "<tr><td>".$rs->Fields("Branch")."</td>";
    $rs->movenext();
}
$rs->Close();   
?>
What I wanted to achieve is to have an "else" statement though I know its not possible using the where statement. Which one is equivalent of it in where statement?
我想要实现的是有一个“else”语句,尽管我知道使用 where 语句是不可能的。哪一个与 where 语句中的它等效?
while (!$rs->EOF){ 
    echo "<tr><td>".$rs->Fields("Branch")."</td>";
    $rs->movenext();
}
if(!$rs->EOF)
{
    echo "<tr><td> Branch is missing</td>";
}
$rs->Close();   
?>
I tried using "if" I didn't get any errors though it didn't print what I wanted to print
我尝试使用“如果”我没有收到任何错误虽然它没有打印我想要打印的内容
回答by Tieme
While-Else does not exists in php.
While-Else 在 php 中不存在。
You could use:
你可以使用:
if ($rs->EOF) {
    echo "<tr><td> Branch is missing</td>";
} else {
    while (!$rs->EOF){ 
        echo "<tr><td>".$rs->Fields("Branch")."</td>";
        $rs->movenext();
    }
}
$rs->Close(); 
回答by Connor
I would like to suggest a slightly different way to approach this, it makes a bit more sense in my head:
我想建议一种稍微不同的方法来解决这个问题,它在我的脑海中更有意义:
if (!$rs.EOF()) {
    while (!$rs.EOF()) {
        // code...
    }
}
else {
    // code...
}
I think it makes more sense to me, because you generally want your expected outcome be handled within the if block, and the other outcome(s) to be handled in the else block.
我认为这对我来说更有意义,因为您通常希望在 if 块中处理预期结果,而在 else 块中处理其他结果。
回答by lonesomeday
while (!$rs->EOF){means "carry on doing this until $rs->EOFis true". When it ends, $rs-EOFwill always be true (otherwise the loop wouldn't have ended), so the conditional will never pass.
while (!$rs->EOF){意思是“继续这样做直到$rs->EOF是true”。当它结束时,$rs-EOF将始终为真(否则循环不会结束),因此条件永远不会通过。
You need to do a test at some point (possibly beforethe whileloop) to see if there are any results found. Without knowing what library you are using, it's impossible to say how to do this.
您需要在某一时刻(可能是为了做一个试验之前的while循环),看看是否有发现任何结果。在不知道您使用的是什么库的情况下,不可能说如何做到这一点。
回答by Some programmer dude
You should check that the result-set is not empty beforeattempting to loop through it.
在尝试循环之前,您应该检查结果集是否为空。
E.g. something like this pseudo-code:
例如像这样的伪代码:
rs = sql("SELECT ...");
if (rs.isEmpty())
    print "No data";
else
{
    while (!rs.EOF())
    {
        ...
    }
}
回答by Chandresh M
I think below code would help you,
我认为下面的代码会帮助你,
<?php while (!$rs->EOF){ 
     if($rs->Fields("Branch"))
     {
      echo "<tr><td>".$rs->Fields("Branch")."</td>";
     $rs->movenext();
     }else{
      echo "<tr><td> Branch is missing</td>";
     }
}
$rs->Close();   
?>

