方法将多次返回FALSE

As the title says I have a lot of conditional statements in one of my methods. Of which I am stuck on my two return FALSE on two of my conditions.

I am using MVC so in my controller I check if the method returns TRUE :

if ($this -> m_homepage -> reset_pw($step = 1, $value)) { // If all is true in method redirect
    $this -> session -> set_flashdata('message', 'A Message Was Sent');
    redirect('c_homepage/index', 'location');
} elseif ($this -> m_homepage -> reset_pw($step = 1, $value) == 'badcap') { // If captcha fails return bad_recaptcha
    var_dump("bad_recaptcha");
} else { // if any other FALSE happens (only one more left) then that means account is locked
    var_dump("account is locked");
}

and in my method :

if (!$cap -> is_valid) {// If reCaptcha not valid
    return 'badcap';
} else {// If reCaptcha is valid
    if ($lockedaccount == '1') {// If account is locked (1)
        return FALSE; // No email should be sent period.
    } else {
        if ($no_employee) {// email does not exist then 

            ................ // Set up email body and what not

            if ($email_sent() { // If email is sent
                $this -> session -> unset_userdata('email');
                return TRUE;
            }
        }
    }
}

So in my method notice how I have a FALSE statement, and statement that returns a string. How do I distinguish in my controller which one is returned?

I thought doing something like this :

if ($this -> m_homepage -> reset_pw($step = 1, $value) == 'badcap') {
    // This will allow me to execute code is the recaptcha (badcap) is returned
}

Is my logic here incorrect? Any help would be great.

Edit 1

var_dump($this -> m_homepage -> reset_pw($step = 1, $value)); 

gives me badcap

I don't really get what your problem is. You're wondering if using

if ($my_return == 'badcap')
{ 
// CODE
}

Is correct or not ?

If so, then yes, its an okay way to do it.

To make your code clearer, you could also use constants to name your return values, but it doesn't really apply in your case since you have only 3 possible return. You'll think about it when creating a function with 50 different possible return values.


The main problem in your code is that you call your function two times, which is bad for your performances.

You should assign a variable first and use it in your IFs

$return_val = $this -> m_homepage -> reset_pw($step = 1, $value);
if ($return_val == TRUE) ...
if ($return_val == 'badcap')

Also, as you apply the return value and you're sure which type it will be, using === increase performances.

Know that any non-empty string or any non-zero integer is always evaluated as true if taken as a boolean. So, the error is with the first line:

if ($this -> m_homepage -> reset_pw($step = 1, $value))

Even if your function returns the value 'badcap', this would evaluate to true and this condition would be implemented and next if else conditions will be ignored. Instead, use

if ($this -> m_homepage -> reset_pw($step = 1, $value) == TRUE)

Pass one more variable along with false or true.

    if (!$cap -> is_valid) {
         return array('badcap','False'); 
       } else {
        if ($lockedaccount == '1') {
           return array('lockedaccount','False'); 
        } else {
        if ($no_employee) {// email does not exist then 

            if ($email_sent() { // If email is sent
                $this -> session -> unset_userdata('email');
                return array('mailsent','True'); 
            }
         }
      }
    }