-4

I cannot figure out whats wrong with this segment of code:

}else{
$error_message = '<span class="error">';
foreach($error as $key => $values){
    $error_message.= "$values<br />";
}
$error_message.="</span><br /><br />";
}

I get the error:

Warning: Invalid argument supplied for foreach() in FILE DIRECTORY on line 45

Line 45 is the foreach($error... line

Solved: My issue was I messed up the placement of curly brackets which in turn messed up the array

4

2 回答 2

5

错误消息说这$error不是一个数组。您可以var_dump($error);在循环之前检查它以检查它是什么。

于 2012-04-16T15:56:15.527 回答
0

如果$error有时为空,那么以下内容就足够了:

}else{
    if(!empty($error)){ // only attempt to build error messages if there are errors
        $error_message = '<span class="error">';
        foreach($error as $key => $values){
            $error_message.= "$values<br />";
        }
        $error_message.="</span><br /><br />";
    }
}
于 2012-04-16T17:13:37.500 回答