php - for loop inside a while loop, correct syntax?

本秂侑毒 提交于 2019-12-06 05:17:26

Nothing wrong with having a for inside a while.

Your "for" would be better a bit clearer as

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

I don't see any issues whatsoever with your php.

I copied your code exactly and there was no infinite loop at all.

The result I got was as follows:

<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.

My only suggestion would be to change:

$string = $string.'1';

to

$string .= '1';
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!