Memory Refresh

Control structures

Programming Regex, Antipatterns, Php, Git

Ternary operator

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


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);


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 } } }


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

Programming Regex, Antipatterns, Php, Git