【问题标题】:PHP do while with multiple while conditionsPHP用多个while条件做while
【发布时间】:2017-06-05 22:53:50
【问题描述】:

我希望有人能在这里帮助我。我看了看,发现有人遇到了同样的问题: Do-While Loop with Multiple Conditions

但是我已经检查过了,看不到哪里出错了

这是我的代码:

$x=0;
$y=0;
do {
$x = rand(0,5);
$y++;
} while ($x!=5 || $y<=5);

所以我期待以下内容:当 X 不是 == 5 或 y 大于 5 时代码停止。但是我得到的结果是 x=5 和 y=>5

为了验证理论,我将代码更改为:

$x=0;
$y=0;
echo'<br />--x-y';
do {
    $x = rand(0,5);
    $y++;
    echo'<br />--'.$x.'-'.$y;
    if($y>5)
        echo"-I should finish here";
} while ($x!==5 || $y<=5);
echo "<br />x=".$x;
echo "<br />y=".$y;

在一种情况下,我最终得到了:

--x-y
--5-1
--3-2
--4-3
--4-4
--2-5
--4-6-I should finish here
--1-7-I should finish here
--3-8-I should finish here
--3-9-I should finish here
--3-10-I should finish here
--3-11-I should finish here
--5-12-I should finish here
x=5
y=12

关于我哪里出错了有什么建议吗?

【问题讨论】:

    标签: while-loop multiple-conditions


    【解决方案1】:

    您想在(X = 5 or Y &gt; 5) 时停止。 while 里面的条件是为了继续,所以你必须将条件反转为:(X != 5 AND Y &lt;= 5)

    De Morgan Laws

    【讨论】:

    • 非常感谢!就在这后面的最后一个问题上:我如何检查变量 isset 以及值或变量 IE:while(!isset($json->meta->code) AND $json->meta->code !=200 AND $retires!=3) 如果 $json->meta->code 没有设置我得到一个错误:(
    • 没关系,我将 $json->meta->code 分配给 do 循环内的变量并改为检查该值 :)
    猜你喜欢
    • 1970-01-01
    • 2012-09-09
    • 1970-01-01
    • 1970-01-01
    • 2015-10-24
    • 1970-01-01
    • 2011-11-02
    • 2015-12-16
    • 1970-01-01
    相关资源
    最近更新 更多