Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In php, should I return false, null, or an empty array in a method that would usually return an array?

Tags:

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 false, null, or an empty array in a method that would usually return an array, but has a failure occur?

In other words, if another developer jumped in on my project, what would they expect to see?

like image 495
xtraorange Avatar asked Jul 18 '12 07:07

xtraorange


People also ask

Does an empty array return false PHP?

Use PHP's empty() function. It returns true if there are no elements in the array. empty is the opposite of boolean false.

Is array empty in PHP?

An empty array is falsey in PHP, so you don't even need to use empty() as others have suggested. PHP's empty() determines if a variable doesn't exist or has a falsey value (like array() , 0 , null , false , etc).

How do you declare a null array in PHP?

Syntax to create an empty array:$emptyArray = []; $emptyArray = array(); $emptyArray = (array) null; While push an element to the array it can use $emptyArray[] = “first”. At this time, $emptyArray contains “first”, with this command and sending “first” to the array which is declared empty at starting.

Can I return NULL in PHP?

As of PHP 7.1 you can specify a nullable return type with a question mark in front of the return type. This means that you can either return an integer or a null value. Note that not returning anything also technically means returning a null value.


2 Answers

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.

like image 68
deceze Avatar answered Sep 27 '22 22:09

deceze


I would strongly discourage to return mixed type return values. I consider it to be so much a problem, that i wrote a small article about not returning mixed typed values.

To answer your question, return an empty array. Below you can find a small example, why returning other values can cause problems:

// This kind of mixed-typed return value (boolean or string), // can lead to unreliable code! function precariousCheckEmail($input) {   if (filter_var($input, FILTER_VALIDATE_EMAIL))     return true;   else     return 'E-Mail address is invalid.'; }  // All this checks will wrongly accept the email as valid! $result = precariousCheckEmail('nonsense'); if ($result == true)   print('OK'); // -> OK will be given out  if ($result)   print('OK'); // -> OK will be given out  if ($result === false)   print($result); else   print('OK'); // -> OK will be given out  if ($result == false)   print($result); else   print('OK'); // -> OK will be given out 

Hope this helps preventing some misunderstandings.

like image 36
martinstoeckli Avatar answered Sep 27 '22 22:09

martinstoeckli