1

我需要能够捕捉到错误。我有以下代码

if($call['status_id'] != '' && $call['datetime_required'] != '')
{
   //do stuff
}
else
{
  // tell them how it failed
}

我将如何显示 ti 失败的部分。因此,例如,我可以返回动态错误消息,即

return 'You did not fill in the field $errorField';

在哪里

$errorField

是它失败的 if 检查。

更新

我目前这样编码

if($call['status_id'] != '')
{
    if ($call['datetime_required'] != '')
    {
      //do stuff
    }
    else
    {
      // tell them it failed due to the second condition
    }
}
else
{
   // tell them it failed due to the first condition
}

但是想在一行中进行检查并根据 ti 失败的位置更改消息。

注意@jack 在此更新之前已经发布了他的答案。

4

2 回答 2

3

我不确定我是否完全理解你,你的意思是这样的吗?

function check($call, $req_fields) {
    $failed = array();
    foreach($req_fields as $field) {
        if(!$call[$field]) {
            $failed[] = $field;
        }
    }
    if(count($failed)) {
        return join(', ', $failed) . ' are required.';
    }
    return 'success?' ;
}

$message = check($call, array('status_id', 'datetime_required'));
于 2013-10-03T15:43:18.740 回答
2
if($call['status_id'] != '')
{
    if ($call['datetime_required'] != '')
        //do stuff
    }
    else
    {
        // tell them it failed due to the second condition
    }
}
else
{
  // tell them it failed due to the first condition
}
于 2013-10-03T15:43:33.130 回答