PHP 使用多个 while 条件执行 while

PHP do while with multiple while conditions

我希望有人可以帮助我。我看了一下,发现有人遇到了同样的问题: 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

对我哪里出错有什么建议吗?

您想在 (X = 5 or Y > 5) 时停止。 while里面的条件是continue,所以需要把condition倒过来:(X != 5 AND Y <= 5)

De Morgan Laws