PHP: Function with 2 or more return values
Question by Compi | 2017-05-16 at 20:05
Is PHP providing any possibility with which a function is able to return more than one single result value? For example, I have the following function:
function calcValues($x, $y) { return $x + $y; }
As soon as the return line is reached, the whole function stops. With this, it is only possible to produce one return value. However, I need several ones. Is there any way to do that?
Related Topics
JavaScript: Sort Array of Numbers
Tutorial | 0 Comments
PHP: Upload of large Files
Tutorial | 0 Comments
PHP: Current Date and Time
Tutorial | 0 Comments
jQuery: Send HTML5 Canvas to Server via Ajax
Tutorial | 0 Comments
PHP: Fill Array with Sequence of Numbers or Characters
Tutorial | 0 Comments
Create URL for Website from Title of Page
Tutorial | 0 Comments
Important Note
Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.
Participate
Ask your own question or write your own article on askingbox.com. That’s how it’s done.
A function can return an array. With this, it is possible to transmit more than one return value at once because an array can have an arbitrary length.
Your function could look like that, for example:
You will get the same result with:
And you can call the function in the following way:
As you can see, we pass two numbers to this function here. The function returns an array in which the sum as well as the product of the two figures are stored. Within the function, the array is created and after calling the function, we can read out and output the single elements of the array.
2017-05-16 at 22:08