2013-01-09 86 views
0

需要幫助。計數循環

我需要的是當循環它將計數達到零後有多少循環。

$variable=50;//this is not fixed 
$loop=0; 
//------------------// 
$variable-10; 
//the $variable becomes 40 
//$loop becomes 1 
//repeat the process 
$variable-10; 
//the $variable becomes 30 
//$loop becomes 2 
//until the variable reaches zero then 
//$loop becomes 5 
+4

[你有什麼嘗試?](http://www.whathaveyoutried.com/)請參閱[FAQ](http://stackoverflow.com/faq)。 –

+2

我需要一輛新車。你可以幫我嗎? –

+0

你從現在開始嘗試過什麼是我的口號。文字優美。 – Khez

回答

0
$variable = 50; 
$loop = 0; 
while($variable > 0) 
{ 
    $variable -= 10; 
    $loop++; 
} 
0

實現內部循環

$count=0; 
for ($variable=50; $variable<=0; $variable-=10){ 
$count++; 
} 
0

代碼高爾夫您的變量$變量= 50!

for($v=50,$c=0;($v-=10)>0;++$c); 
echo $c; 

在這個很短的腳本的末尾,$c將代表的倍代碼部分的在執行循環的數目。本質上,它也表示增量序列被執行的次數。

這與執行循環的測試的次數形成對比,由於在出現之前的最後一種情況,該次數固有地高1。