繁体   English   中英

PHP的-在while循环内循环,正确的语法?

[英]php - for loop inside a while loop, correct syntax?

我有这个循环

while (count($arr) < 7)
        {

            $string = 'xx';
            for ($i=0; strlen($string) < 4; $i++)
            {
                $string = $string.'1';
            }
            echo "<br>array length is less than 7, word $string is created.";
            $arr[] = $string;
        }

每次我运行这段代码时,我的xampp本地服务器都会超时,并给服务器未找到错误。

我发现如果删除内部的for循环,它将运行良好。 for循环放入while循环有什么问题吗?

另外,我在for循环中的条件语句strlen($string) < 4没有对$i变量的任何引用,但是我看不到有没有与计数器无关的条件语句的任何逻辑。 我错了吗,是否需要与柜台进行某种比较?

TIA

一段时间内没有问题没有错。

您的“ for”会更好,因为

while(strlen($string) < 4) {
    $string = $string.'1';
}

我看不到您的php有任何问题。

我完全复制了您的代码,根本没有无限循环。

我得到的结果如下:

<br>array length is less than 7, word xx11 is created.
<br>array length is less than 7, word xx11 is created.
<br>array length is less than 7, word xx11 is created.
<br>array length is less than 7, word xx11 is created.
<br>array length is less than 7, word xx11 is created.
<br>array length is less than 7, word xx11 is created.
<br>array length is less than 7, word xx11 is created.

我唯一的建议是更改:

$string = $string.'1';

$string .= '1';

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM