Dyl*_*ill 2 php regex controls loops nested
我的代码的输出应该是:
*
**
***
****
*****
Run Code Online (Sandbox Code Playgroud)
我目前正在使用带有嵌套for循环的代码来获取结果.
for($lineNumber=1;$lineNumber<=5;$lineNumber++) {
for($starCount=1;$starCount<=$lineNumber;$starCount++) {
echo("*");
}
echo("<br />");
}
Run Code Online (Sandbox Code Playgroud)
我需要能够在没有嵌套for循环的情况下获得相同的结果,而且我很难过.我唯一想要使用的是单个for循环.没有其他的.没有ifs,开关或其他循环.
谢谢!
$str = '';
for($lineNumber=1;$lineNumber<=5;$lineNumber++) {
$str = $str . '*';
echo $str;
echo("<br />");
}
Run Code Online (Sandbox Code Playgroud)
使用此字符串累加器消除了对第二个循环的需要.