Memory App
 



Programming > Php > Basics > Control structures   ZCE 5.3  |  Patterns  |  Modern Php

Ternary operator


echo 10 == $x ? 'YES' : 'NO';
(1/4)

Switch

Evaluates the initial expression only once ($a in this case). Then compares it against the values.

switch($a) {

    case 1: // do one
        break;

    case 2: // do two
        break;

    default: // do zero
}
(2/4)

While

Even if the condition never evaluates to true, the contents of the loop will be executed at least once.

// this ouput nothing
$i = 10;
while ($i < 10) {
    echo 1;
    $i++;
}

// this ouput 1
$i = 10;
do {
    echo 1; 
    $i++;
} while ($i < 10);
(3/4)

Break

It takes an optional parameter, which allows you to exit from multiple nested loops

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.

Questions