Recently, I was trying to use if condition inside foreach loop .. and it has been done without issues..
But, when I use else inside the loop .. the if condition becomes fully ignored and the else condition will be executed without checking the if condition ..
Here is the loop :
foreach ($ids as $id) {
if (substr($id, 0, 8) === $first8) {
$matched = true;
header("Location: success.php");
break;
} else {
echo "<script language=\"JavaScript\">\n";
echo "alert('Entered ID is incorrect');\n";
echo "window.location='index.html?loginFailed=true&reason=wrongID'";
echo "</script>";
exit;
}
}
Now when the user enter his ID.. the else condition will be executed without if condition.. weather the ID is correct or not .. Noting that when I remove the else the code is working perfectly if the ID is correct..
Any ideas?
$first8var?$first8is. Also, useexitordieafterheader(), otherwise the code will still run which you do not want since you useheader()