2012-10-06 39 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