SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast
    Join Date
    Oct 2011
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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!

  2. #2
    SitePoint Member
    Join Date
    Oct 2011
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    can you supply the code of the function in question?

  3. #3
    SitePoint Enthusiast
    Join Date
    Oct 2011
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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;

  4. #4
    SitePoint Wizard bronze trophy Immerse's Avatar
    Join Date
    Mar 2006
    Location
    Netherlands
    Posts
    1,661
    Mentioned
    7 Post(s)
    Tagged
    1 Thread(s)
    You could use the 'identical' operator:

    PHP Code:
    $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.

  5. #5
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    In effect your code is more likely to look like this:

    PHP Code:

    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

    PHP 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:

    PHP Code:
    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;
     }



Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •