made “foreach”

Posted by – March 21, 2008

This may sound obvious but I still want to mention it :D

<?php
$a = array(
1=>'B',
2=>'C',
3=>'D',
0=>'A',
4=>'E',
);
echo "Using for\n";
$c = count($a);
for($i=0;$i<$c;$i++){
    echo $a[$i],"\n";
}
echo "Using foreach\n";
foreach($a as $k){
    echo $k, "\n";
}
?>
Output is

Using for
A
B
C
D
E
Using foreach
B
C
D
A
E

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>