For Loop vs Foreach Loop in PHP (Simple Example)


// For Loop

for ($i = 1; $i <= 10; $i++) {

echo $i;

}

// Foreach Loop

$array = array("sample" => "value", "sample2" => "value2", "sample3" => "value3");

foreach ($array as $key => $val) {

echo $key  . ': ' .  $value . '<br />';

}
This entry was posted in PHP. Bookmark the permalink.

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>