How PHP functions return multiple values

Source: Internet
Author: User
But my experience is as follows:
The following function tells us whether the two users can communicate with each other.

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

Now that things have changed, in addition to telling you if you can communicate, tell me why , that is, you want the function call to return FALSE to tell the caller why it is false.
I used the following method: (to pass a reference parameter to the function why, and then return it)

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

There is another way to return an array: (but so the return value is not a simple Boolean value, it will change how the function is called)

function A_can_talk_to_b ($a, $b) {    if (...) {        $why = ' ... ';        Return array (' status ' = True, ' why ' = $why);    } else {        $why = ' ... ';        Return array (' status ' = False, ' why ' = $why);}    }

Is there any other way besides these two?

Reply content:

But my experience is as follows:
The following function tells us whether the two users can communicate with each other.

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

Now that things have changed, in addition to telling you if you can communicate, tell me why , that is, you want the function call to return FALSE to tell the caller why it is false.
I used the following method: (to pass a reference parameter to the function why, and then return it)

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

There is another way to return an array: (but so the return value is not a simple Boolean value, it will change how the function is called)

function A_can_talk_to_b ($a, $b) {    if (...) {        $why = ' ... ';        Return array (' status ' = True, ' why ' = $why);    } else {        $why = ' ... ';        Return array (' status ' = False, ' why ' = $why);}    }

Is there any other way besides these two?

Yes, well, there's no problem from a language point of view.
Does the landlord want to ask this kind of practice good or not?
My advice is that it's best not to do this, because every time you have to pass a parameter that seems to have nothing to do with logic, the person who uses it will find it strange.

If you can, throw exception, you can get unexpected information by catch exception

This design requirement is inherently unreasonable.
A function, just do one thing.
If you want to do multiple things, separate functions to do.
Instead of putting it in a function, return to the array at the beginning. No other tricks.

return {' Cantalk ' =>true, ' why ' = ' Balabala ... '}

It's good to be right, and if you're going to use why elsewhere, it's also necessary to make a separate package.

If you don't care to return true or false handy
can return code
And then find out why, based on the Code=>message table.

Returning arrays, JSON objects 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.