Coding basics: always return one data type

For dynamic data type languages (PHP, JavaScript, etc.), when you return a certain data type from a function, always return that data type even if there's no data to return.  If a function is expected to return an integer value, but there's no value to return, return 0.  If the function returns an object, return NULL if it's invalid.  If the function returns a string then return an empty string if there's nothing to return.   For example:

The code that calls the fmt_name() function doesn't have to worry about the data type.  If no name was returned, the data could still be treated as a string even when empty.  If a NULL value were returned instead of an empty string, the code wouldn't have been able to jump right into that switch statement that checks the string length.

Here's an example for arrays:

Because an array is always returned from the get_foods() function, we can immediately traverse the results with no worries of data types.  If we couldn't always expect an array, we'd have more cluttered code to check what data type was returned.  In this example, if no foods were found, the function returns an empty array, which naturally skip past the foreach loop.  If a NULL value was returned, or BOOL, you'd have to deal with that.

There are situations where you need to return a mixed data type, but in general, return one data type for empty or failed routines.  It keeps code clean and less complicated.