InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

Menu
  • Home
  • Sitemap

Python Programming Language Best Tutorials and Code Examples

Learn Python Right Now!
Home
PHP
Returning More Than One Value in PHP
PHP

Returning More Than One Value in PHP

InfinityCoder November 23, 2016

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

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

1
2
3
4
5
6
7
8
9
10
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);

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:

1
2
3
4
5
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:

1
2
3
4
5
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:

1
2
3
4
5
6
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.

Share
Tweet
Email
Prev Article
Next Article

Related Articles

Finding PEAR Packages
You want a listing of PEAR packages. From this list …

Finding PEAR Packages

Sorting Multiple Arrays in PHP
You want to sort multiple arrays or an array with …

Sorting Multiple Arrays in PHP

About The Author

InfinityCoder
InfinityCoder

Leave a Reply

Cancel reply

Recent Tutorials InfinityQuest

  • Adding New Features to bash Using Loadable Built-ins in bash
    Adding New Features to bash Using Loadable …
    June 27, 2017 0
  • Getting to the Bottom of Things in bash
    Getting to the Bottom of Things in …
    June 27, 2017 0

Recent Comments

  • fer on Turning a Dictionary into XML in Python
  • mahesh on Turning a Dictionary into XML in Python

Categories

  • Bash
  • PHP
  • Python
  • Uncategorized

InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

About Us

Start learning your desired programming language with InfinityQuest.com.

On our website you can access any tutorial that you want with video and code examples.

We are very happy and honored that InfinityQuest.com has been listed as a recommended learning website for students.

Popular Tags

binary data python CIDR convert string into datetime python create xml from dict python dictionary into xml python how to create xml with dict in Python how to write binary data in Python IP Address read binary data python tutorial string as date object python string to datetime python

Archives

  • June 2017
  • April 2017
  • February 2017
  • January 2017
  • December 2016
  • November 2016
Copyright © 2021 InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more
Programming Tutorials | Sitemap