【发布时间】: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