PHP:为什么这是一个无限循环?

时间:2013-04-25 14:51:45

标签: php loops while-loop combinations infinite-loop

$x = 0;
$y = 0;

while ($x < 6);
{
    while ($y < 6)
    {
    echo "INSERT INTO map_location(xLoc,yLoc) VALUES ($x,$y)"; 
    $y++;
    }
    $x++;
}

我正在尝试使用PHP脚本批量生成SQL语句,以生成从0到6的x,y坐标的所有可能组合。这应该是总共49种组合,我不想插入它们逐一。我正在努力弄清楚为什么这是一个无限循环。

3 个答案:

答案 0 :(得分:5)

本声明:

while ($x < 6);

基本上是这样的:

while ($x < 6) { }

这是无休止无限循环。

答案 1 :(得分:2)

你的第一个while之后似乎有一个分号,从逻辑上讲,

$x = 0; 
while ( $x < 6 ); 
// the above statement is always going to be true because x = 0 (and 0 is less than 6), hence, always going to loop.

删除分号,您应该设置。

答案 2 :(得分:1)

试试这个:

for ($x=0;$x<=6;$x++)

    for ($y=0;$y<=6;$y++)

         echo "INSERT INTO map_location(xLoc,yLoc) VALUES ($x,$y) <br />";