How does a php function return multiple values?

Source: Internet
Author: User
However, my experience is as follows: the following function tells us whether two users can communicate with each other. {Code...} Now, things have changed. In addition to telling whether to communicate or not, we also need to tell why, that is, if you want function calls to return false, you can tell the caller what is false. I used the following method... but my experience is as follows:
The following function shows whether two users can communicate with each other.

function a_can_talk_to_b($a, $b) {    return (...) ? true : false;}

Now, things have changed. In addition to notifying you whether you can communicate with each otherWhy?That is, you want to tell the caller what is false when the function call returns false.
I used the following method: (pass a reference parameter why to the function and return it)

function a_can_talk_to_b($a, $b, &$why) {    if (...) {        return true;    } else {        $why = '...';        return false;    }}

Another way is to return an array: (but the returned value is not a simple boolean value, which will change the function call method)

function a_can_talk_to_b($a, $b) {    if (...) {        $why = '...';        return array('status' => true, 'why' => $why);    } else {        $why = '...';        return array('status' => false, 'why' => $why);    }}

Are there other methods besides these two methods?

Reply content:

However, my experiences are as follows:
The following function shows whether two users can communicate with each other.

function a_can_talk_to_b($a, $b) {    return (...) ? true : false;}

Now, things have changed. In addition to notifying you whether you can communicate with each otherWhy?That is, you want to tell the caller what is false when the function call returns false.
I used the following method: (pass a reference parameter why to the function and return it)

function a_can_talk_to_b($a, $b, &$why) {    if (...) {        return true;    } else {        $why = '...';        return false;    }}

Another way is to return an array: (but the returned value is not a simple boolean value, which will change the function call method)

function a_can_talk_to_b($a, $b) {    if (...) {        $why = '...';        return array('status' => true, 'why' => $why);    } else {        $why = '...';        return array('status' => false, 'why' => $why);    }}

Are there other methods besides these two methods?

Well, there is no problem in terms of language.
Is this a good practice?
My suggestion is, it is best not to do this, because each time we have to upload another parameter that seems unrelated to the logic, it may be strange to use it.

If possible, throw an exception. You can use catch Exception to obtain the unexpected information.

This design requirement is unreasonable.
One function does one thing.
To do multiple tasks, separate functions.
To put it in a function, return an array at the beginning. No other moves.

return {'canTalk'=>true,'why'=>'balabala...'}

It is quite correct. If you use why elsewhere, it is also necessary to propose an encapsulation separately.

If you do not care about the convenience of returning true or false
CODE returned
Find the why according to the CODE => MESSAGE table.

The returned array and JSON object can solve your problem.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.