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