php - Returning foreach value from function to array -
i have function , passing array argument in validate it. on next page inserting array argument. below:
function check_subject($sub_array) { foreach($sub_array $value) { if (!empty($value)) { if (!preg_match('/^[0-9a-za-z ]{2,20}$/', $value)) { return "not supported format"; } } } } other page inserting $errors[] = check_subject($g8_sub_vali); , storing return result in $error[] array.
than printing error if else somehitng below..
if($errors) { foreach ($errors $msg) { if(!empty($msg)) { echo " - $msg<br />\n"; } } } else { echo "there no error"; } but not getting else loop value "there no error". think there problem in returning function. can please suggest me?
since making:-
$errors[] = check_subject($g8_sub_vali); this line append null array in case making check valid entry because function return null.
so if want prevent should do:-
$err = check_subject($g8_sub_vali); if(empty($err)==false) $errors[] = $err; this give valid values of errors , in else loop if there no error.
Comments
Post a Comment