2
I would like to know, when I should use these kinds of conditions, I have already made the test returns me the same thing, but what is the difference? I believe it is not only decrease lines, but let’s combine the second option is cleaner right?
public function method($param) {
$sql = "SELECT * FROM `table` WHERE field = :field";
$count = $this->_db->select($sql, array('method' => $param[1]));
if ($count->rowCount() > 0) {
return true;
} else {
return false;
}
}
Here’s the second one with ternary condition.
public function method($param) {
$sql = "SELECT * FROM `table` WHERE field = :field";
$count = $this->_db->select($sql, array('method' => $param[1]));
return $count->rowCount() > 0 ? true : false;
}
What promotes difference in my code?
You don’t even need the ternary, you can just come back:
return $count->rowCount() > 0
.– rray
@rray is that I will use in another function, in a vip system, if vip returns true if not false, true executes a query, and false executes another.
– user94336
I think what he meant was, by just putting
return $count->rowCount() > 0
will already returntrue
orfalse
– Max Rogério