2012-10-06 38 views
1

對象的號碼,我想按照這個模式來打印對象的數量打印:如何根據PHP

If there is 1 object, print 0 
If there is 2 objects, print 0 1 
If there is 3 objects, print 0 1 2 

我嘗試下面的代碼:

for($i = count($nodes) ; $i >= 0 ; $i--){ 
print $i; 
} 

但結果是:

If there is 1 object, print 0 1 
If there is 2 objects, print 0 1 2 
if there is 3 objects, print 0 1 2 3 

哪我不能使用。我怎樣才能做到這一點?

回答

3

你應該使用這樣的:

$n = 3; // where n is no. of object 
for($i=0; $i<$n; $i++){ 
    print $i." "; 
    } 
//output 0 1 2 
+0

知府,謝謝。 – Zim3r