2013-08-24 28 views
0

正如標題所示,我的一個方法中有很多條件語句。其中我的兩個條件都卡在我的兩個return FALSE上。方法將多次返回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; 
      } 
     } 
    } 
} 
在我的方法通知

所以,我怎麼有虛假陳述,並返回一個字符串的語句。我如何在控制器中區分哪一個返回?

我以爲做這樣的事情:

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

給我badcap

回答

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') 

而且,由於應用的返回值和你確定哪些類型將是,使用===增加表演。

0

知道任何非空字符串或任何非零整數總是作爲布爾值計算爲true。因此,誤差與第一行:

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

即使你的函數返回值「badcap」,這將評估爲真,這個條件將得到實施和未來,如果其他條件都將被忽略。相反,使用

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

傳遞一個變量以及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'); 
      } 
     } 
     } 
    } 
+0

不知道可能是我誤解你的問題..!讓我知道..!! –