Conditions, loops and functions

if, foreach, and typed functions.

PHP
<?php
function grade(int $marks): string {
    if ($marks >= 75) {
        return "Distinction";
    }
    return $marks >= 50 ? "Pass" : "Fail";
}

foreach ([80, 60, 30] as $m) {
    echo $m . ": " . grade($m) . "\n";
}

Compare with === and not ==. The double form converts types first, which leads to surprising results.

โ† Getting started with PHPArrays โ†’