Posted 2019-11-05 with tags PHP, PHP 7.1, type safety

By preceding the type declaration with a question mark (?), PHP allows returning a null or void value:

function get_thing($thing): ?string {
    if ( ! empty($thing)) {
        return $thing;
    } else {
        return null;
    }
}

  Tags: