Write a FOR loop in PHP that prints the numbers 0-9 to the page

I wonder why this thread got that much replies. Is it only because the problem was quite easy or it really needs that much examples to print 0 to 9 numbers in a print using for loop in PHP? :slight_smile:

$loopy = array("0. Don't", "1. be", "2. such", "3. a", "4. spoilt", "5. sport!", "6. Let", "7. them", "8. enjoy", "9. it!");

function test_print($item2)
{
    echo substr($item2,0,1);
}

array_walk($loopy, test_print);

What a great thread! It’s interesting to see how many different ways there are to do even a very simple thing in php.

$n = '';
for($i = 0; $i<=9; $i++) {
    echo strlen($n);
    $n = $i.$n;
}

Nice idea :smiley:

for($i=0;$i<9;print strlen($n.=$i++));
for($i=print(0);$i<=9;print($i++));

for (
	$time = (string) strtotime('Fri, 13 Feb 2009 23:31:30 GMT'),
	$time = substr($time, -1).substr($time, 0, -1),
	$len  = strlen($time),
	$pos  = 0;
	$pos < $len;
	print($time[$pos++])
);

Any solution using Friday the 13th has got to be worth a good mark. :eye:

I’m surprised there has been no binary shenanigans!


<?php
for($i=0;$i<=9;$i++){
$aData = explode('2','02121021121002101211021112100021001');
echo bindec($aData[$i]).'<br />';
}
?>

Mine was kind of binary… just used for loops to generate the binary string then convert :stuck_out_tongue:


$i = 0;
do {
    echo "$i ";
    $i++;
} while ($i <= 9); // $i < 10 will print 10.

Off Topic:

Too bad there’s no longer Funniest Thread of The Year award. :smiley:

Meets the technical requirements but obviously not the intent.



try{
	for($i='Q'; $i<17; $i++) {
		throw new Exception('Monkey');
	}
}
catch (Exception $e) {
	echo '0123456789';
}

only you forgot to output ‘0’ :stuck_out_tongue:

Rather a strange way of doing it, but interesting anyway :slight_smile:

We should have ‘obscure ways of solving problems’ threads…

Ah, yes. 0-9 I edited it to help anyone in the future looking for this very useful solution. :slight_smile:

Some simple solutions:

 
 
for ($ord = 48; $ord < 58; $ord++) {
    echo chr($ord);
}
 
for ($nums = range(9, 0); !is_null($n = array_pop($nums)); print($n));
 
for (print('0123456789'); 0;);
 
for ($str = '0123456789', print($str); isset($str);) {
    unset($str);
}
// var:
for ($str = '0123456789', print($str), $str = ''; isset($str);)

No OOP yet?!


Class Counter {
	public $count = 0;
	function countMe() {
		echo $count;
		return this;
	}
	function addMe() {
		$this->count++;
	}
}

$counter = new Counter();
for($i = 0; $i < 10; $++) {
	$counter->countMe()->addMe();
}

(thorougly untested)

Also, people posting do and do/while loops will be banned from this forum for not following the thread rules. You are providing misleading information to newcomers :slight_smile: .

This thread is starting to get really inspiring.

I nominate it for highlighting. :slight_smile:

Delvar-
I fixed yours up so it runs, and put it all into a for loop:


 
for ($i = 0; $i < 10; $i++) {     
    if (!isset($counter)) {
        class Counter 
        {     
            public $count = 0;     
            function countMe() 
            {         
                echo $this->count;         
                return $this;     
            }     
            function addMe() 
            {
                $this->count++;     
            } 
        }
        $counter = new Counter(); 
    }  
    $counter->countMe()->addMe(); 
}

I had no idea you could declare a class inside a for loop. That’s frightening.

Yeh, although it’s fairly pointless since PHP doesn’t have lexical scope (yet).

Ha, and still no reply from the OP. :smiley:

He is probably in a state of shock!