我需要能够发现错误。我有以下代码
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($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在此更新之前已经发布了他的答案。
答案 0 :(得分: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'));
答案 1 :(得分: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
}