Php for循环有2个变量?

is it possible to do this? (here is my code)

for ($i = 0 ; $i <= 10 ; $i++){
  for ($j = 10 ; $j >= 0 ; $j--){
     echo "Var " . $i . " is " . $k . "<br>";
  }
}

I want something like this:

var 0 is 10

var 1 is 9

var 2 is 8 ...

But my code is wrong, it gives a huge list. Php guru, help me !!

7个回答

Try this:

for ($i=0, $k=10; $i<=10 ; $i++, $k--) {
    echo "Var " . $i . " is " . $k . "<br>";
}

The two variables $i and $k are initialized with 0 and 10 respectively. At the end of each each loop $i will be incremented by one ($i++) and $k decremented by one ($k--). So $i will have the values 0, 1, …, 10 and $k the values 10, 9, …, 0.

doujingke4981
doujingke4981 感谢您提供此代码段。 没想到你可以做这样的事......
接近 11 年之前 回复
dongshun7312
dongshun7312 根据我所知道的人,它确实在Java中起作用。
接近 11 年之前 回复
douxie9347
douxie9347 这也适用于C.
接近 11 年之前 回复
duansai1314
duansai1314 只有极少数情况下我会想要使用它,但我完全把这个放在我的工具带中。
接近 11 年之前 回复
dongwang3066
dongwang3066 谢谢,100%工作
接近 11 年之前 回复
douna1892
douna1892 是。 C ++和C#肯定,我非常肯定C和Java,但我尽量避免它,因为它可以快速无法读取。
接近 11 年之前 回复
duan051347
duan051347 我的下一个问题:你能用C,C ++,Java和C#做到这一点吗?
接近 11 年之前 回复
dongshanya2008
dongshanya2008 鉴于for循环的语法意味着什么,这是有道理的......
接近 11 年之前 回复
duanchen7703
duanchen7703 哇哇 我不知道你在for循环中设置了多个变量。 尼斯。
接近 11 年之前 回复
doulai2025
doulai2025 +1 - 甚至不知道你能做到这一点!
接近 11 年之前 回复

If, as your code looks like, you have two values running the opposite direction you could simply substract:

echo "Var " . $i . " is " . 10 - $i . "<br>";

But I guess that's not really what you want? Also, be careful with the suggested comma operator. While it is a nice thing it can cause naughty side effects in other languages like C and C++ as PHP implements it differently.

You could also add a condition for the second variable

for ($i=0, $k=10; $i<=10, $k>=0 ; $i++, $k--) {
    echo "Var " . $i . " is " . $k . "<br>";
}



你不应该使用两个for循环来实现你想要实现的目标,因为你总共循环了121次(11x11 )。 你真正想要的只是在循环之外声明一个跟踪j的计数器,然后在循环中递减j。</ p>

编辑:感谢Gumbo为我捕获包含。< / p>
</ div>

展开原文

原文

You shouldn't be using two for-loops for what you'd like to achieve as you're looping 121 times total (11x11). What you really want is just to have a counter declared outside of the loop that tracks j, and then decrement j inside the loop.

Edit: Thanks Gumbo for catching the inclusion for me.

dtczp02204
dtczp02204 啊,是的,甚至没有看到。
接近 11 年之前 回复
dpv50040
dpv50040 事实上它是11·11 = 121(从0到10,包括0和10)。
接近 11 年之前 回复



要扩展其他(正确)答案,您所做的就是称为嵌套</ em>循环。 这意味着对于外循环(第一个)的每次迭代,您都在完成整个内循环。 这意味着您可以获得 11 + 11 + 11 + ... = 11 * 11 </ code>输出</ p>
</ div>而不是11个输出

展开原文

原文

To expand on the other (correct) answers, what you were doing is called nesting loops. This means that for every iteration of the outer loop (the first one), you were completing the entire inner loop. This means that instead of 11 outputs, you get 11 + 11 + 11 + ... = 11 * 11 outputs

array_map(function($i) {
    echo "Var {$i} is ".(10-$i)."<br/>".PHP_EOL; 
}, range(1,10));

I tried to get a start and end time and store in the database, given a start and end time, you loop through each time using two variables i&j

   $start = "09:00";
   $end = "18:00";
   $strEnTim = strtotime("10.00");

   $slotStart = strtotime($start);
   $slotEnd = strtotime($end);
   $slotNow = $slotStart;

   for( $i=$slotStart, $j=$strEnTim; $i, $j<=$slotEnd; $i+=3600,  $j+=3600) 
   {
        if(( $i < $slotNow) && ( $j < $strEnTim)) continue;
        Slot::create([
            'start_time' => date("H:i",$i),
            'end_time' => date("H:i", $j)
        ]);
   }
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐