Php 另一个while循环中的while循环只运行一次
嗨,我对编码是相当陌生的,对stackoverflow社区也是全新的,所以请容忍我 我正在尝试生成一个代码,该代码将创建以下输出: a0 B0B1 b2 a1 B0B1 b2 a2 b0 b1 b2 使用此代码:Php 另一个while循环中的while循环只运行一次,php,while-loop,nested-loops,Php,While Loop,Nested Loops,嗨,我对编码是相当陌生的,对stackoverflow社区也是全新的,所以请容忍我 我正在尝试生成一个代码,该代码将创建以下输出: a0 B0B1 b2 a1 B0B1 b2 a2 b0 b1 b2 使用此代码: <?php $count1 = 0; $count2 = 0; while ($count1 < 3){ echo "a".$count1."<br>"; while ($count2 < 3){
<?php
$count1 = 0;
$count2 = 0;
while ($count1 < 3){
echo "a".$count1."<br>";
while ($count2 < 3){
echo "b".$count2." ";
$count2 ++;
}
$count1++;
}
?>
我的问题是嵌套的while循环只运行一次,并提供:
a0
B0B1 b2 a1
a2
我想要的输出可以通过使用for循环或其他方法来实现,但我很好奇为什么这不起作用。这也是应该通过数据库查询运行的代码的早期阶段,我只看到了带有while循环的示例
感谢您的帮助。您需要在每次循环
count1
时重置count2
像这样:
<?php
$count1 = 0;
$count2 = 0;
while ($count1 < 3){
echo "a".$count1."<br>";
while ($count2 < 3){
echo "b".$count2." ";
$count2 ++;
}
$count2 = 0;
$count1++;
}
?>
您需要在每次outter循环运行之间“重置”$count2的值。请注意$count2=0:
<?php
$count1 = 0;
$count2 = 0;
while ($count1 < 3){
echo "a".$count1."<br>";
while ($count2 < 3){
echo "b".$count2." ";
$count2 ++;
}
$count1++;
$count2 = 0;
}
?>
您需要重置计数器。您不需要在时间之外定义变量,只需在第一个变量中定义即可
$count1 = 0;
while ($count1 < 3){
echo "a".$count1."<br>";
$count2 = 0;
while ($count2 < 3){
echo "b".$count2." ";
$count2 ++;
}
$count1++;
}
$count1=0;
而($count1<3){
回显“a”。$count1。“
”;
$count2=0;
而($count2<3){
回声“b”.$count2.“;
$count2++;
}
$count1++;
}
通过内循环1次后,$count2
为3
,因此通过外循环的第2次/3次(3<3)
为假。如果您想让它再次运行,您需要重置$count2
。当然,干杯:)
$count1 = 0;
while ($count1 < 3){
echo "a".$count1."<br>";
$count2 = 0;
while ($count2 < 3){
echo "b".$count2." ";
$count2 ++;
}
$count1++;
}