for cycle php code example

Example 1: php for

<?php
/* example 1 */

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

/* example 2 */

for ($i = 1; ; $i++) {
    if ($i > 10) {
        break;
    }
    echo $i;
}

/* example 3 */

$i = 1;
for (; ; ) {
    if ($i > 10) {
        break;
    }
    echo $i;
    $i++;
}

/* example 4 */

for ($i = 1, $j = 0; $i <= 10; $j += $i, print $i, $i++);
?>

Example 2: loop through php array

foreach (array_expression as $value)
    statement
foreach (array_expression as $key => $value)
    statement

Example 3: php loop through array

foreach($array as $item=>$values){
     echo $values->filepath;
    }

Tags:

Php Example