Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Return Replacement for else

Case One

Wrong

$name = 'Lucas';
$greeting = null;

if (is_null($name)) {
    $greeting = 'No name was provided';
} else {
    $greeting = 'Hello '. $name;
}

echo $greeting;

Right

$name = 'Lucas';
$greeting = 'No name was provided';

if ($name) {
    $greeting = 'Hello '. $name;
}

echo $greeting;

Case Two

Wrong

function greeting($name = null) {
    if (is_null($name)) {
        return 'No name was provided.';
    } else {
        return 'Hello '.$name;
    }
}

Right

function greeting($name = null) {
    if (is_null($name)) {
        return 'No name was provided.';
    }

    return 'Hello '.$name;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.