Expert Refresh

Control structures

1) In which of the following the contents of the loop is exectued at least once?





2) How you exit from two nested loops?







If

Ternary operator <?php echo 10 == $x ? 'YES' : 'NO';

Switch

A switch statement evaluates the initial expression ($a in this case) only once, and then compares it against the individual case values. <?php switch($a) { case 1: // do one break; case 2: // do two break; default: // do zero }

While / Do

These two types of loop are very similar. In a do ... while() loop, the check takes place at the end of each iteration of the loop - meaning that, even if the condition never evaluates to true, the contents of the loop will be executed at least once. <?php // this ouput nothing $i = 10; while ($i < 10) { echo 1; $i++; } // this ouput 1 $i = 10; do { echo 1; $i++; } while ($i < 10);

For / Foreach

Break

It takes an optional parameter, which allows you to exit from multiple nested loops <?php for ($i=0; $i<=10; $i++) { for ($j=0; $j<=10; $j++) { if (($j + $i) % 5 == 0) { break 2; // Exist this loop and the next one } } }

Continue

Like with break, you can provide it an integer parameter to specify the level of nesting to which the it applies


References