Re: [PHP] function returning true or errors
On Thursday, April 18, 2002, at 11:56 AM, Jason Wong wrote: > What I tend to do is define functions like so: > > function doo($dah, $dib, &$error) { > ... > I had not even thought about passing an extra parameter by reference -- great idea. 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] function returning true or errors
Rather than using true and false you can use 1, 0 it saves key strokes, reduces script size, etc Also use ' instead of " if you don't need it evaluated by PHP. if (custom_function) { print 'Custom Function succeeded!'; } else { print 'There was a problem!'; } One thing I've done in the past is return a variable with a message and an error code for example function check_sheet() { // if first name is less than 2 characters fail check and provide an error if (strlen($_POST['firstname']) < '2') { $return['status'] = '0'; $return['message'][] = 'First name must be at least 2 characters long'; } // use if instead of elseif so we can return multiple error messages at once if (strlen($_POST['lastname']) < '2') { $return['status'] = '0'; $return['message'][] = 'Last name must be at least 2 characters long'; } // if we have not encountered an error set return status to 1 (true) if (!isset($return['status']) { $return['status'] = '1'; } return $return; } Now in my main script I would perform checking like this // execute check_sheet function $check_sheet = check_sheet(); if ($check_sheet['status'] == '1') { // if the function returned 1 (true) display confirmation print 'Thank you for your submission!'; } else { // if the function returned 0 (false) display error messages for ($i=0; $i'; } } unset ($check_sheet); This is example code written in my client for reference. This code will allow you to return multiple error messages without echoing html code from a function. -Original Message- From: Erik Price [mailto:[EMAIL PROTECTED]] Sent: Thursday, April 18, 2002 9:25 AM To: [EMAIL PROTECTED] Subject: [PHP] function returning true or errors 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 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] function returning true or errors
On Thursday 18 April 2002 23:25, Erik Price wrote: > 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? What I tend to do is define functions like so: function doo($dah, $dib, &$error) { ... ... if success { return TRUE; } else { $error = "You've committed a grave sin!"; return FALSE; } } Then call as: if (doo(...)) { echo "OK"; } else { echo "Error: $error"; } -- Jason Wong -> Gremlins Associates -> www.gremlins.com.hk Open Source Software Systems Integrators * Web Design & Hosting * Internet & Intranet Applications Development * /* Now I lay me down to sleep, I pray the Lord my soul to keep, If I should die before I wake, I'll cry in anguish, "Mistake!! Mistake!!" */ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] function returning true or errors
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