PROGRAMMING

m9/ PHP
REMEMBERS




Last update:   23-07-2021

While

With do/while the contents of the loop will be executed at least once. Even if the condition never evaluates to true.
 
// this ouput nothing
$i = 10;
while ($i < 10) {
    echo 1;
    $i++;
}

// this ouput 1
$i = 10;
do {
    echo 1; 
    $i++;
} while ($i < 10);
... 6 lines
 

Break / Continue

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
        }
    }
}
... 1 lines
 

Switch

Evaluates the initial expression only once ($a), then compares it against the values.
 
switch($a) {

    case 1: // do one
        break;

    case 2: // do two
        break;

    default: // do zero
}
... 1 lines
 

Conditional assigment

Ternary operator
 
$x = expr1  ? expr2 : expr3; // x = expr2 if expr1 = TRUE
Null coalescing
 
$x = expr1 ?? expr2 ; // x = expr1 if expr1 exists and not null

Questions    
Reference

        A B C D E F
🔔
1/3