In general, it is preferable to assign the result of a function that you are likely to repeat with a variable.
In the example that you suggested, the difference in the processing code generated by this approach and the alternative (multiple function calls) would be insignificant. However, when the function in question is more complex, it would be better not to re-execute it.
For instance:
for($i=0; $i<10000; $i++) { echo date('Ym-d'); }
It runs in 0.225273 seconds on my server, and:
$date = date('Ym-d'); for($i=0; $i<10000; $i++) { echo $date; }
executes 0.134742 seconds. I know that these fragments are not quite equivalent, but you understood this idea. For many months or years, many pages are loaded by many users, even a difference of this size can be significant. If we were to use some kind of complex function, serious scalability problems could be introduced.
The main advantage of not assigning a return value to a variable is that you need another line of code. In PHP, we can usually complete our task at the same time as calling our function:
$sql = "SELECT..."; if(!$query = mysql_query($sql))...
... although this is sometimes discouraging for reasons of readability.
In my opinion, for consistency, assigning return values ββto variables is generally the best approach, even when performing simple functions.
source share