Function returing either boolean or string value

Hi,

I have a requirement where I want function to return either error message in case of failure or true if success. I have done this but the problem is when I compare the returned value of true I have o use “TRUE” in stead of just TRUE. There is another way like return empty string like “” if there is no error. But is this the best way of implementing error or no error from the function?

Example - I would like to check if the user is valid user if yes then return true or else return error message. By returning error message from function, I would like to track all error messages in my one common script.

Thanks!

can you supply the code of the function in question?

Code is something like below
function test()
{
initiate db connection;
execute query (select * from user where userid = ‘a’ and pwd = ‘b’);
check record count;
if (record count == 1)
return true;
else
return “some error message”;

close db connection;
}

calling above function
initiate class object;
call function and store returned value;
if (returned value == “TRUE”)
–do some action
else
echo “returned message from function”;

Thanks;

You could use the ‘identical’ operator:


$result = my_function();

if(true === $result) {
    echo "Woo! Success!";
} else {
    echo "Oh no, it went wrong: " . $result;
}

Note the ‘===’ instead of the regular ‘==’. Using this will mean you’re checking for boolean true. A string with value ‘TRUE’ won’t match true when using the === operator.

A little more complicated, but you could also look into throwing exceptions if something goes wrong in your function.

In effect your code is more likely to look like this:



if( !userCredentialsValid($user, $pass) ){  // return true or false 

// well, OK, now something has gone wrong 
// did you really pass an empty $user or $pass to the function?
// or did the user send 1MB of data as $user?
// did the username contravene your username rules?
// or did the user /pass combo just not find any matches?

}


The question is how complicated should userCredentialsValid() actually be?

At the moment its job and name is “are the user/pass valid or not?” - its a yes no question, so provide a yes/no response.

If the user commits a blank password, then you should be snagging that fact higher up in your code, if the username contains chars like “? < )” and they are against your rules, then you should be snagging that higher too…

Does userCredentialsValid() need to return a variety of messages? Not in its current state. So leave the responsibility to create a message to the calling code


if( false === userCredentialsValid($user, $pass) ){  // arguably more readable, nod to immerse...
relocateWithMessage("Those credentials were incorrect, try again.") ;
}

Maybe in your mind you see a function whose job (and therefore name) is:


function userCredentialsValidateThenSubmitOrGetMessage($user, $pass){

// a pile of security checks

$msgs = array(
// a pile of different text strings for each occasion of an error 

);

 if(error is found){
  return $msg[33];
 }else{
  return true;
 }
}