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?
This is a perfectly fine way to write this logic. If you return from an
ifbody, you can definitely omit theelse.It would be more concise, however, to just do this: