【问题标题】:Codeigniter: function return statement not returningCodeigniter:函数返回语句不返回
【发布时间】:2016-02-25 14:06:50
【问题描述】:

我正在尝试使用以下函数返回一个 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 或 else only
  • 正如@safinchacko 所说,你所说的没有意义。在您的“第二代码” 中,您有一个if/else,每个都有一个return,这意味着将显示一个代码块或另一个代码块。您确定您在第二个代码中同时获得了 "Im here""Im here also" 输出吗?
  • {"err":1,"code":1,"msg":"Error"} 这是来自调用函数。这表示它返回错误对象而不是成功对象。
  • @CodeGodie 我完全同意这没有意义,因此我发布并需要知道为什么会发生这种情况以及如何解决它。
  • 知道了,只需要确保您正确发布。嗯……这个方法在哪里? is_defined_headtype()是控制器还是模型方法?

标签: php apache codeigniter ubuntu


【解决方案1】:

嗨,我认为错误在这里 你正在做一个只有一个'='的 if 语句,这是为了设置一个值,但你想比较,所以应该是:

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

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 什么是“提示”类型,以前从未听说过。
    • 有多个if条件。您能否给出更明确的答案,或者一小段示例代码来说明这如何回答问题?
    猜你喜欢
    • 2011-10-26
    • 2011-06-06
    • 1970-01-01
    • 2021-09-27
    • 2014-02-19
    • 1970-01-01
    相关资源
    最近更新 更多