代码点火器:函数返回语句不返回


Codeigniter: function return statement not returning

我正在尝试使用以下函数返回一个 db 值,以防失败,该函数应该返回一个错误对象。我已经用 If/else 语句分隔了代码块。if 条件满足,则执行 if 块。但是由于某种原因,if 块中的 return 语句不执行,代码执行继续到下一个语句。我在两种情况下都尝试了这两个选项,结果是相同的。

第一个代码

public function is_defined_headtype($head_id)
{
    if ($is_defined = $this->ci->account_head_model->is_defined_headtype($head_id))
    {
        echo "Im here<br>";
        return $this->_success($is_defined, ACC_SUCCESS);
    }
    echo "Im here also";
    // General Error Occurred
    return $this->_general_error();
}

第二个代码

public function is_defined_headtype($head_id)
{
    if ($is_defined = $this->ci->account_head_model->is_defined_headtype($head_id))
    {
        echo "Im here<br>";
        return $this->_success($is_defined, ACC_SUCCESS);
    }
    else
    {
        echo "Im here also";
        // General Error Occurred
        return $this->_general_error();
    }
}

在这两种情况下,我都得到了以下输出。

Im here
Im here also{"err":1,"code":1,"msg":"Error"}

调用函数定义

public function add_eam($user_id, $eamname, $entityid, $headid, $eamdescription, $eamisfraction, $eampercentage, $eamamount, $affectedheadid)
{
    if (/* $eam_id = $this->ci->account_eam_model->insert_eam($user_id, $eamname, $entityid, $headid) */ true)
    {
        $is_defined = $this->is_defined_headtype(/* $headid */1);
        echo json_encode($is_defined);
        if($is_defined == 1)
        {
            echo "Im here";
            exit();
            if ($entity_id = $this->ci->account_eam_definition_model->insert_eam_definition(/* $user_id */ 1, $eam_id, $eamdescription, $eamisfraction, $eampercentage, $eamamount, $affectedheadid))
            {
                return $this->_success($eam_id, ACC_ENTITY_ADDED);
            }
            else 
            {
                return $this->_general_error();
            }
        }
        echo "Im not here";
        exit();
        return $this->_success($eam_id, ACC_ENTITY_ADDED);
    }
    // General Error Occurred
    return $this->_general_error();
}
嗨,

我认为错误在这里您正在执行一个只有一个"="的 if 语句,这是为了设置一个值,但您希望 comapre,因此应该是:

"=="以比较两个值是否相同 "!="以查看两个值是否不同

public function is_defined_headtype($head_id)
{
    if ($is_defined = $this->ci->account_head_model->is_defined_headtype($head_id))
    {
        echo "Im here<br>";
        return $this->_success($is_defined, ACC_SUCCESS);
    }
    echo "Im here also";
    // General Error Occurred
    return $this->_general_error();
}

If 语句中尝试键入杂耍(int)或使用 === 与双方数据进行强比较。