开发者

Returning two function from function

I am calling a search function from my object and i开发者_运维问答n return i want to send the result plus some message so can i return two variable one with array and one with the message like $data and $message

$this->query=("select * from user where pno='".$search."'");
$rd = $this->executeQuery();
return @$data = $rd->fetch_assoc();

return $message;

when it get back to

$result=$user->search($result);

how can i fetch the result from it Thanks


put your variables in an array and return the array.

so your function can return this for example:

return array($data, $message);

then you can call your function like this:

list($data, $message) = search('hello');


While @moe's answer is perfectly correct, you probably don't want to do that for error handling.

Why?

Because next you'll find yourself having to return a two-element array from *every* function with the real data in one element and a possible error in the other, and then you're going to need to check the error every single time, even if you can't do anything about it.

That's gonna get tedious really quick.

You're dealing with errors, so treat them like errors! When an error happens, throw an exception. If the calling code can elegantly recover from an error, it can look for and expect to deal with the exception. If the calling code can't recover from an error, then the exception will crawl up the chain until it hits a default exception handler, which you can configure to elegantly and delicately handle the responsibility of telling the user that something broke.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜