Php 循环10次,然后增加一个值,然后重复这个过程总共10次?
我需要x(0)用一个值增加10倍,然后我需要给y加一个值,让x从0再次增加。 基本上我正在创建一个网格 现在我手动做一行,循环10次,然后手动改变y,再次循环,重复。我想把它自动化Php 循环10次,然后增加一个值,然后重复这个过程总共10次?,php,loops,Php,Loops,我需要x(0)用一个值增加10倍,然后我需要给y加一个值,让x从0再次增加。 基本上我正在创建一个网格 现在我手动做一行,循环10次,然后手动改变y,再次循环,重复。我想把它自动化 $int = 0; $x = 0; $y = 0; $z = 0; while($int < 10) { echo 'posX="'. $x .'" posY="'. $y .'" posZ="'. $z .'<br>'; $int++; $x+=20; } $int=
$int = 0;
$x = 0;
$y = 0;
$z = 0;
while($int < 10) {
echo 'posX="'. $x .'" posY="'. $y .'" posZ="'. $z .'<br>';
$int++;
$x+=20;
}
$int=0;
$x=0;
$y=0;
$z=0;
而($int<10){
回声“posX=”.$x.“posY=”.$y.“posZ=”.$z.“
”;
$int++;
$x+=20;
}
我现在手动做的是将y的值更改为20,然后让循环再次运行,我必须手动更改10次
有什么建议吗?你可以试试这样的方法。我使用变量
$x_inc
和$y_inc
来定义在循环的每个过程中增加多少$x
和$y
:
$x = $y = $z = 0;
$x_inc = 20;
$y_inc = 20;
for ($i = 0; $i < 10; $i++) {
for ($j = 0; $j < 10; $j++) {
echo 'posX="'. $x .'" posY="'. $y .'" posZ="'. $z .'<br>';
$x += $x_inc;
}
$y += $y_inc;
$x = 0;
}
$x=$y=$z=0;
$x_inc=20;
$y_inc=20;
对于($i=0;$i<10;$i++){
对于($j=0;$j<10;$j++){
回声“posX=”.$x.“posY=”.$y.“posZ=”.$z.“
”;
$x+=$x_公司;
}
$y+=$y_公司;
$x=0;
}
我个人会使用模数,它总是感觉是处理此类“定时”增量的最佳方式。您可以阅读有关模运算符的内容。这也意味着您不必有嵌套循环
$int = 11;
$x = 0;
$y = 0;
$z = 0;
while($int < 110) {
echo '"posX="'. $x .'" posY="'. $y .'" posZ="'. $z .'"<br />"';
$int ++; //you probably want to do this last unless you need int to increment before we evaluate it
$x+=20;
if(($int % 10) == 0) { //basically if int is a multiple of 10 you want to add to y and reset x
$y += 10; //your value
$x = 0; // reset x to 0 for the next 10 iterations
}
}
$int=11;
$x=0;
$y=0;
$z=0;
而($int<110){
echo“'posX=“”.$x.”“posY=“”.$y.”“posZ=“”.$z.”“
”;
$int++;//您可能希望最后一次执行此操作,除非在我们计算它之前需要int递增
$x+=20;
if($int%10)==0{//基本上,如果int是10的倍数,则要将其添加到y并重置x
$y+=10;//您的值
$x=0;//在接下来的10次迭代中将x重置为0
}
}
======或========
$int = 0;
$x = 0;
$y = 0;
$z = 0;
while($int < 110) {
echo '"posX="'. $x .'" posY="'. $y .'" posZ="'. $z .'"<br />"';
$int ++; //you probably want to do this last unless you need int to increment before we evaluate it
$x+=20;
if(($int / 10) == 1) { //basically if int is a multiple of 10 you want to add to y and reset x
$y += 10; //your value
$x = 0; // reset x to 0 for the next 10 iterations
}
}
$int=0;
$x=0;
$y=0;
$z=0;
而($int<110){
echo“'posX=“”.$x.”“posY=“”.$y.”“posZ=“”.$z.”“
”;
$int++;//您可能希望最后一次执行此操作,除非在我们计算它之前需要int递增
$x+=20;
如果($int/10)==1{//基本上如果int是10的倍数,则要加上y并重置x
$y+=10;//您的值
$x=0;//在接下来的10次迭代中将x重置为0
}
}
不知何故,这需要重置x,因为现在我得到的是:很抱歉,我在你的问题中忽略了这一点,它改变了你的操作方式。(见编辑)