PHP Functions Returning More Than One Value - Supercoders | Web Development and Design | Tutorial for Java, PHP, HTML, Javascript PHP Functions Returning More Than One Value - Supercoders | Web Development and Design | Tutorial for Java, PHP, HTML, Javascript

Breaking

Post Top Ad

Post Top Ad

Thursday, May 23, 2019

PHP Functions Returning More Than One Value

PHP Functions




Returning More Than One Value

Problem

You want to return more than one value from a function.

Solution

Return an array and use list() to separate elements:

          function array_stats($values) {
                 $min = min($values);
                 $max = max($values);
                 $mean = array_sum($values) / count($values);

                 return array($min, $max, $mean);
          }

          $values = array(1,3,5,9,13,1442);
          list($min, $max, $mean) = array_stats($values);

Discussion

From a performance perspective, this isn’t a great idea. There is a bit of overhead because PHP is forced to first create an array and then dispose of it. That’s what is happening in this example:

          function time_parts($time) {
                 return explode(':', $time);
          }

          list($hour, $minute, $second) = time_parts('12:34:56');


You pass in a time string as you might see on a digital clock and call explode() to break it apart as array elements. When time_parts() returns, use list() to take each element and store it in a scalar variable. Although this is a little inefficient, the other possible solutions are worse because they can lead to confusing code.

One alternative is to pass the values in by reference. However, this is somewhat clumsy and can be nonintuitive because it doesn’t always make logical sense to pass the necessary variables into the function. For instance:

          function time_parts($time, &$hour, &$minute, &$second) {
                 list($hour, $minute, $second) = explode(':', $time);
          }

          time_parts('12:34:56', $hour, $minute, $second);


Without knowledge of the function prototype, there’s no way to look at this and know $hour, $minute, and $second are, in essence, the return values of time_parts().

You can also use global variables, but this clutters the global namespace and also makes it difficult to easily see which variables are being silently modified in the function. For example:

          function time_parts($time) {
                 global $hour, $minute, $second;
                 list($hour, $minute, $second) = explode(':', $time);
          }

          time_parts('12:34:56');


Again, here it’s clear because the function is directly above the call, but if the function is in a different file or written by another person, it’d be more mysterious and thus open to creating a subtle bug.

Our advice is that if you modify a value inside a function, return that value and assign it to a variable unless you have a very good reason not to, such as significant performance issues. It’s cleaner and easier to understand and maintain.



No comments:

Post a Comment

Post Top Ad