Php 方法将多次返回FALSE
正如标题所说,我的一个方法中有很多条件语句。其中有两种情况下,我的两个Php 方法将多次返回FALSE,php,return-value,Php,Return Value,正如标题所说,我的一个方法中有很多条件语句。其中有两种情况下,我的两个返回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 S
返回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
我不明白你的问题是什么。您想知道是否使用
if ($my_return == 'badcap')
{
// CODE
}
正确与否
如果是的话,那么是的,这是一个好办法
为了使代码更清晰,您还可以使用常量来命名返回值,但它实际上并不适用于您的情况,因为您只有3种可能的返回。在创建一个具有50个不同可能返回值的函数时,您会考虑这个问题
代码中的主要问题是调用函数两次,这对性能不利 您应该首先分配一个变量,并在IFs中使用它
$return_val = $this -> m_homepage -> reset_pw($step = 1, $value);
if ($return_val == TRUE) ...
if ($return_val == 'badcap')
另外,在应用返回值并确定返回值的类型时,使用
==
可以提高性能。知道,如果将任何非空字符串或非零整数作为布尔值,则其计算结果始终为true。因此,错误出现在第一行:
if ($this -> m_homepage -> reset_pw($step = 1, $value))
即使您的函数返回值'badcap',该值也将计算为true,并且将实现该条件,然后将忽略if-else条件。相反,使用
if ($this -> m_homepage -> reset_pw($step = 1, $value) == TRUE)
再传递一个变量以及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');
}
}
}
}
不确定我是否误解了你的问题。。!!让我知道。。!!
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');
}
}
}
}