return $result ?: []; what is the explanation of this code in details please.
2 Answers
The explanation to this code is really simple. But to understand it, you have to know about ternary operator first. Check the below example.
(condition) ? (if true, do this) : (otherwise, do this)
In your statement return $result ?: [];
$result
is your condition which is either true or false (in this case return is checking if $result returns something or not)
If $result returns true (meaning it has a value) then do nothing because there is no condition after ?
as you can see in return $result ?: [];
But if $result returns false (meaning it don't have any value) then assign an empty array [] to it which is shown by the do this section
after the :
In conclusion, if the $result has a value then that value is being returned but if it does not have any value then an empty array will be returned. I hope this helps!
This is the Ternary Operator, this code check $result having the value or not,if it has value return $result Else return empty array.