PHP的-在while循环内循环,正确的语法? [英] php - for loop inside a while loop, correct syntax?

查看:70
本文介绍了PHP的-在while循环内循环,正确的语法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个循环

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本地服务器都会超时,并显示未找到服务器错误.

Every time I run this part of the code, my xampp local server would time out and gives the server not found error.

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

I have found that if I delete the inner for loop, it would run fine. Is there anything wrong with putting the for loop inside the while loop?

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

Also, my conditional statement strlen($string) < 4 in the for loop does not have any reference to the $i variable, but I don't see any thing not logical of having a conditional statement not related to the counter. Am I wrong, does it require some sort of comparison against the counter?

TIA

推荐答案

一段时间内没有for没错.

Nothing wrong with having a for inside a while.

您的"for"会更清晰一些

Your "for" would be better a bit clearer as

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

这篇关于PHP的-在while循环内循环,正确的语法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆