Reputation: 1
Is it good to use && as shorthand for if only statement in PHP? I like its simplicity but is it a good practise to use it?
Example: $a == "foo" && $b = "bar" ;
Is it more like if($a == "foo" && $b = "bar"){}
than if($a == "foo"){ $b = "bar" }
?
If so, could it cause any problems?
Upvotes: 0
Views: 474
Reputation: 9153
The term for what you describe is called branchless condition. That is not best practice, because of it's bad readability.
But there are some cases where it is very practical and good readable like
$logger && $logger->Log('Log my message');
which will call $logger->Log('Log my message')
only if $logger
is truthy like
if ($logger) {
$logger->Log('Log my message');
}
Upvotes: 0