I am reorganizing an extensive overtime code base. In the end, we will develop the whole system in classes, but at the same time I will use the opportunity to improve my PHP skills and improve some of the obsolete codes that we use on several hundred websites.
Over time, I read controversial articles about how best to return data from a user-defined function, usually debates fall into two categories: those that relate to best technical practice, and those who are concerned about the ease of reading and presentation.
I am interested in the opinions (with clarification) about what you consider best practice when returning from a user-defined PHP function.
I do not know which of the following should be considered as the best standard for using this basic theoretical function, for example:
Approach a.
Filling the returned variable and returning it at the end of the function:
<?php function theoreticalFunction( $var ) { $return = ''; if( $something > $somethingelse ){ $return = true; }else{ $return = false; } return $return; } ?>
Approach b.
Return at each endpoint:
<?php function theoreticalFunction( $var ) { if( $something > $somethingelse ){ return true; }else{ return false; } } ?>
A possible duplicate might be What is the best PHP practice for using functions that return true or false? however, this is not limited to just true or false, despite the fact that my main example is above.
I looked at the PSR guidelines but didn't see anything (but maybe I missed it, so please feel free to tell me the PSR with a link :)).
Extension of the original question:
Is the method used to return depending on the expected / desired type of output?
Does this method change depending on the use of procedural or object-oriented programming methods? As this question shows, object orientation brings its own eccentricities to further expand the possible formatting / presentation options. Recommendations on return methods in PHP
Please try to be clear in your explanations, I'm interested in WHY you choose your preferred method and what, if you like, made you choose it using a different method.