When returning values in php, is it considered good or bad practice to return mixed data types. I\'m working on a project where I am constantly faced with methods that return a
This answer needs an update. There is a better solution as of PHP 7.1:
This is what I do if I want to return a string but it might be null:
function myFunction(): ?string{
//do things here and return string or null
}
The ? before the string essentially means null or string as the return type. This allows you to have a limited return type but still allows null. This works for all types.
You can read more here PHP Manual - returning values