PHP的循环与2个变量? [英] Php for loop with 2 variables?
问题描述
$ b $ pre $
for($ i = 0; $ i <= 10; $ i ++){$ b ($ j = 10; $ j> = 0; $ j - ){
echoVar$ b。 $ i。 是。 $ k。 <峰; br> 中;
$ / code $ / pre
我想要这样的东西:
<0> var 0是10
<1> var 1是9
<2> var 2是8 ...
但是我的代码是错误的,它给出了一个巨大的列表。 Php guru,帮助我!!
解决方案试试这个:
<$ p ($ i = 0,$ k = 10; $ i <= 10; $ i ++,$ k--){
echoVar$ p> $ i。 是。 $ k。 <峰; br> 中;
两个变量 $ i $ c $使用 0
和 10
来初始化c>和 $ k
分别。每个循环结束时, $ i
将会加1( $ i ++
)和 $ k
减1( $ k -
)。所以 $ i
的值分别为0,1,...,10和 $ k
值10,9,... ,0。
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 !!
解决方案 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.
这篇关于PHP的循环与2个变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!