Ibrahim Azhar Armar
Ibrahim Azhar Armar

Reputation: 25745

if else in a php function

i have methods in a php class which returns boolean response the code i have used is.

public function checkIfExist($table ,$key, $value)
{
    $sth = $this->dbh->prepare("SELECT COUNT(*) FROM $table WHERE $key = :value");
    $sth->bindParam(':value', $value);
    $sth->execute();
    if($sth->fetchColumn() >= 1)
    {   
        return true;
    }
        return false;
}

the above method works for me without the else condition being included, logically it should work because once the functions get the true as boolean response it will exit the method. but is it the right way or should i be including else condition there?

Upvotes: 2

Views: 3663

Answers (8)

Naftali
Naftali

Reputation: 146302

That's fine coding.

You don't need the else.

this is because if anything in that if statement was true --> you are returning, and if it is not true --> you are returning.

So the function is correct in its implementation.

Upvotes: 2

andrew
andrew

Reputation: 2879

I use this kind of coding a lot:

function do_something($arg)
{
    if(some_condition_fails($arg))
    {
        return false;
    }

    if(some_other_condition_fails($arg))
    {
        return false;
    }

    //do it
    return $result;
}

There's no need to nest everything in else's...in fact, adding extra blocks around code that doesn't need it is dirtier (in my opinion) than leaving it out.

Upvotes: 0

Infotekka
Infotekka

Reputation: 10423

Just return the result of the comparison:

public function checkIfExist($table ,$key, $value)
{
    $sth = $this->dbh->prepare("SELECT COUNT(*) FROM $table WHERE $key = :value");
    $sth->bindParam(':value', $value);
    $sth->execute();
    return ($sth->fetchColumn() >= 1);
}

Upvotes: 1

Iulius Curt
Iulius Curt

Reputation: 5104

It is right. And also very practiced.

Upvotes: 0

Michael Banzon
Michael Banzon

Reputation: 4967

It is really a matter of preferences.

Personally I like having only one return statement per function/method. This would require the use of a variable to hold the return value - but hopefully the gain is more readable code.

Upvotes: 0

Matt Ball
Matt Ball

Reputation: 359786

This is a perfectly fine way to write this logic. If you return from an if body, you can definitely omit the else.

It would be more concise, however, to just do this:

return $sth->fetchColumn() >= 1;

Upvotes: 6

Daff
Daff

Reputation: 44215

This is fine since as you said the method returns already. Since your condition already returns a boolean you might also write

public function checkIfExist($table ,$key, $value)
{
    $sth = $this->dbh->prepare("SELECT COUNT(*) FROM $table WHERE $key = :value");
    $sth->bindParam(':value', $value);
    $sth->execute();
    return $sth->fetchColumn() >= 1;
}

Upvotes: 1

anubhava
anubhava

Reputation: 785088

Better to have your return like this:

return ($sth->fetchColumn() >= 1);

instead of if and else

Upvotes: 3

Related Questions