Non ti basta un for?
Codice PHP:
for($i=0;$i<=$massimo;$i++){
echo 
$i;