PHP: Where to place the value "false"?

Is one of the following functions better than the other in terms of placement of the return return statement?

Function # 1:

function equalToTwo($a, $b)
{
    $c = $a + $b;
    if($c == 2)
    {
        return true;
    }
    return false;
}

Function No. 2:

function equalToTwo($a, $b)
{
    $c = $a + $b;
    if($c == 2)
    {
        return true;
    }
    else
    {
        return false;
    }
}

Thank!

+5
source share
4 answers

There are no differences between the two functional differences; You must choose the one that is most obvious and readable.

I usually used else.

Please note that your specific example should be written as

return $c == 2;
+9
source

What about:

return ($c == 2);
+2
source

, , .

, , - ...

function do( $var=null ) {

    if ( $var === null ) {
        return false;
    }

    // many lines of code

}

. . , ...

function do( $var=null ) {

    if ( $var !== null ) {
        //many lines of code
    }
    else {
        return false;
    }

}
+2

. -, , .

0

All Articles