I\'ve found several responses to this, but none pertaining to PHP (which is an extremely weak typed language):
With regards to PHP, is it appropriate to return
An array is a collection of things. An empty array would signal that "everything went fine, there just isn't anything in that collection". If you actually want to signal an error, you should return false
. Since PHP is dynamically typed, it's easy to check the return value either strictly or loosely, depending on what you need:
$result = getCollection();
if (!$result) // $result was false or empty, either way nothing useful
if ($result === false) // an actual error occurred
if ($result) // we have an array with content
There are also exceptions for error reporting in exceptional cases. It really depends on the responsibilities of the function and the severity of errors. If the role of the function allows the response "empty collection" and "nope" equally, the above may be fine. However, if the function by definition must always return a collection (even if that's empty) and in certain circumstances it cannot, throwing an exception may be a lot more appropriate than returning false
.