patternphpModerate
PHP ideal way to check function returns true
Viewed 0 times
idealphpwayfunctiontruereturnscheck
Problem
Which of the following way of checking whether a function returns
I was told by a friend that Method B is a good practice, but I believe Method A is better since it checks whether function returns
Method A: (Checks whether function is TRUE and then assigns.)
Method B: (First assigns the value, then checks—two operations.)
TRUE or FALSE is best, in terms of efficiency and code readability?I was told by a friend that Method B is a good practice, but I believe Method A is better since it checks whether function returns
TRUE then assigns.Method A: (Checks whether function is TRUE and then assigns.)
if ($result = $db->getResults($id)) {
echo $result;
} else {
echo 'fail';
}Method B: (First assigns the value, then checks—two operations.)
$result = $db->getResults(90);
if ($result) {
echo $result;
} else {
echo 'fail';
}public function getResults($no) {
if ($no > 85) {
$result = 'pass';
return $result;
} else {
return FALSE;
}
}Solution
Actually, both methods assign the value first before comparing its value. Therefore, in terms of efficiency, there's no difference.
For code readability, you could write extra parentheses around the assignment if you choose method A. Method B has a slight advantage when you have to perform debugging before the branch, i.e.:
The ternary operator I've used above is yet another way to do your branching; its efficiency is in the same ballpark though. Since the variable itself is used solely in the truthy branch you can also use the shortened ternary operator (>= 5.3) like so:
Although ternary operators are a powerful concept, they're easy to abuse as well and can cause either bugs or a WTF during code review. My personal guideline is to start favouring an
For code readability, you could write extra parentheses around the assignment if you choose method A. Method B has a slight advantage when you have to perform debugging before the branch, i.e.:
$res = $db->getResult(95);
var_dump($res); // added for debugging without having to move the code out of the condition
echo $res ? $res : 'fail';The ternary operator I've used above is yet another way to do your branching; its efficiency is in the same ballpark though. Since the variable itself is used solely in the truthy branch you can also use the shortened ternary operator (>= 5.3) like so:
echo $res ?: 'fail';Although ternary operators are a powerful concept, they're easy to abuse as well and can cause either bugs or a WTF during code review. My personal guideline is to start favouring an
if/else construct when either the condition has multiple sub-expressions or when the manual is opened under operator precedence.Code Snippets
$res = $db->getResult(95);
var_dump($res); // added for debugging without having to move the code out of the condition
echo $res ? $res : 'fail';echo $res ?: 'fail';Context
StackExchange Code Review Q#17578, answer score: 10
Revisions (0)
No revisions yet.