Let's examine the flow of control:
The foreach loop goes through the first iteration of the array.
This has the value "Alpha", which is identified as valid.
Local variable $formComplete is set to true.
The function returns true.
What happened?
The rest of the array never was processed.
Now what if the "invalid&#" value was first?
The foreach loop goes through the first iteration of the array.
This has the value "invalid&#", which is identified as invalid.
$arrTwo[$key] is set to "invalid&#", $key is presumably 0
Local variable $formComplete is set to false.
The function returns false.
Again, the rest of the array never gets processed, and $formComplete is quite useless.
My idea is to loop through the entire array first then determine if any data is invalid.
If it is, then we loop through the invalid data again via another array, which unfortunately may be somewhat inefficient.
The function returns true if the form is "complete", i.e. all given data is valid.
Otherwise, it returns false.
This, then, can be used to check if any data is invalid.
Another method is roughly what you described, which involves listing the invalid data on the fly.
In this case, we only need one array:
function checkFieldAlphaNum($array) {
$flag = true;
foreach ($array as $value) {
if (/* invalid data */) {
//error message stating data here
$flag = false;
}
}
return $flag;
}