[PHP] Re: function returning true or errors
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 [EMAIL PROTECTED] wrote in message [EMAIL PROTECTED]">news:[EMAIL PROTECTED]... 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 [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: function returning true or errors
I think this is more along the lines of what was wanted :) function custom_function() { if( $problem1 ) { return 'Problem 1 occured!'; } elseif( $problem2 ) { return 'Problem 2 occured!'; } elseif( ... ) { ... } return true; } if( ($status = custom_function()) === true ) { echo Success; } else { echo $status; } Cheers, Rob. --- Julio Nobrega Trabalhando wrote: 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 [EMAIL PROTECTED] wrote in message [EMAIL PROTECTED]">news:[EMAIL PROTECTED]... 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 [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- .-. | Robert Cummings | :-`. | Webdeployer - Chief PHP and Java Programmer | :--: | Mail : mailto:[EMAIL PROTECTED] | | Phone : (613) 731-4046 x.109 | :--: | Website : http://www.webmotion.com | | Fax : (613) 260-9545 | `--' -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: function returning true or errors
On Thursday, April 18, 2002, at 11:38 AM, Julio Nobrega Trabalhando wrote: function custom_function() { if ($error == 0) { return true; } else { return Error!; } } if (custom_function() != Error!) { echo Success; } else { echo Error; } Your way would work perfect except that I simplified the function I posted, for illustrative purposes -- a mistake, I realize. The returned error message is actually composed of an imploded array, whose contents are entirely dependent on the code in the function (basically it varies depending on circumstances, so I can't write a test for it). I ended up just performing the checks in the script itself, rather than in a function, because I was only using a function as a subroutine to keep my script's switch() statement nice and clean. But in this case, it just was too much trouble (I was also running into a situation where I had to deal with a global variable, etc). Since I don't really need this anywhere else in the script, I don't really even need it to be a function. Thanks for your input though, Julio. Erik Erik Price Web Developer Temp Media Lab, H.H. Brown [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php