Re: function returning true or errors
| From: | Julio Nobrega Trabalhando | Date: | Thu, 18 Apr 2002 15:38:27 +0000 |
| Subject: | Re: function returning true or errors | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-93547@lists.php.net to get a copy of this message | ||
function custom_function()
{
if ($error == 0) {
return true;
} else {
return "Error!";
}
}
if (custom_function() != "Error!") {
echo "Success";
} else {
echo "Error";
}
Also, you could still return false on your custom_function(), if before
the return you $_SESSION['error'] = 'Error!';. Then you could check for
false or true with your if (custom_function()) and echo/unset the
$_SESSION['error'] part.
--
Julio Nobrega.
Um dia eu chego lá:
http://sourceforge.net/projects/toca
Ajudei? Salvei? Que tal um presentinho?
http://www.submarino.com.br/wishlistclient.asp?wlid=664176742884
"Erik Price" <pricee@hhbrown.com> wrote in message
news:741A20E9-52E0-11D6-932F-0050E4857868@hhbrown.com...
>
> I am writing a function that performs some actions. I would like to
> return true if the actions succeed, or return an error message if the
> actions fail. How should I go about it? The following code doesn't do
> it, because the returned error message is interpreted as a boolean
> "true" (I think that's what's happening):
>
> if (custom_function() == true) {
> print "Custom Function succeeded!";
> } else {
> print custom_function();
> }
>
> I would actually rather just have the error message generated by the
> script that calls the function, but the function performs some logic
> that determines what kind of error message to give. I was thinking of
> having the function return "1" if succeeds, "2" if error code A, or
> "3"
> if error code B, and then a switch statement could decide what to do in
> the calling script -- but does this sound sloppy?
>
>
> Erik
>
>
>
>
> ----
>
> Erik Price
> Web Developer Temp
> Media Lab, H.H. Brown
> pricee@hhbrown.com
>