【问题标题】:Method will return FALSE multiple times方法将多次返回 FALSE
【发布时间】:2013-08-24 15:55:40
【问题描述】:

正如标题所说,我的一种方法中有很多条件语句。其中我的两个return FALSE 在我的两个条件下被困住了。

我正在使用 MVC,所以在我的控制器中我检查该方法是否返回 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");
}

在我的方法中:

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;
            }
        }
    }
}

所以在我的方法中,请注意我有一个 FALSE 语句,以及返回字符串的语句。如何在我的控制器中区分返回的是哪一个?

我想这样做:

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

我这里的逻辑不正确吗?任何帮助都会很棒。

编辑 1

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

给我坏帽子

【问题讨论】:

    标签: php return-value


    【解决方案1】:

    我真的不明白你的问题是什么。您想知道是否使用

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

    正确与否?

    如果是这样,那么是的,这是一个不错的方法。

    为了使您的代码更清晰,您还可以使用常量来命名您的返回值,但它并不真正适用于您的情况,因为您只有 3 个可能的返回值。在创建具有 50 个不同可能返回值的函数时,您会考虑到这一点。


    您的代码中的主要问题是您两次调用您的函数,这对您的性能不利。

    您应该首先分配一个变量并在您的 IF 中使用它

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

    此外,当您应用返回值并确定它将是哪种类型时,使用=== 会提高性能。

    【讨论】:

      【解决方案2】:

      知道任何非空字符串或任何非零整数如果作为布尔值总是被评估为真。所以,错误在于第一行:

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

      即使您的函数返回值 'badcap',这也会评估为 true,并且会执行此条件,并且将忽略 next if else 条件。相反,使用

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

      【讨论】:

        【解决方案3】:

        将另一个变量与 false 或 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'); 
                    }
                 }
              }
            }
        

        【讨论】:

        • 不确定我是否误解了你的问题..!!让我知道..!!
        猜你喜欢
        • 2017-10-02
        • 2012-02-11
        • 2015-08-10
        • 1970-01-01
        • 2017-07-26
        • 2011-06-09
        • 1970-01-01
        • 1970-01-01
        • 2021-06-03
        相关资源
        最近更新 更多