Codeigniter:函数返回语句不返回

时间:2016-02-25 14:06:50

标签: php apache codeigniter ubuntu

我试图在失败的情况下使用以下函数返回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();

}

2 个答案:

答案 0 :(得分:0)

嗨,我认为错误就在这里 你正在做一个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();
}

答案 1 :(得分:-1)

如果语句中,尝试键入Juggling (int)或使用===与两侧数据进行强有力的比较。