0

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?

5
  • 2
    If is not ignored, the condition is simply failing Commented Feb 28, 2018 at 12:07
  • 1
    no matter how many ids in the $ids array it will only do one thing Commented Feb 28, 2018 at 12:07
  • What is $first8 var? Commented Feb 28, 2018 at 12:09
  • I'm also wondering what $first8 is. Also, use exit or die after header(), otherwise the code will still run which you do not want since you use header() Commented Feb 28, 2018 at 12:10
  • only array first element will go through the condition. If condition met then redirected to other page, if not you will get elert and redirected to another page .So actually only first data is checked through your code Commented Feb 28, 2018 at 12:11

2 Answers 2

1

Your code check only first element of the array. Use this code:

if(in_array($first8, array_map(function($id){
    return substr($id, 0, 8);
}, $ids))) {
    header("Location: success.php"); 
} 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;    
}
Sign up to request clarification or add additional context in comments.

1 Comment

That was what I am looking for .. Worked perfectly.. Thank you very much.
0

Put your "no matches" process after the loop and exit; if a match is found -- instead of break.

foreach ($ids as $id) {
    if (strpos($id,$first8)===0) {
        header("Location: success.php");
        exit;
    }
}
echo "<script language=\"JavaScript\">\n";
    echo "alert('Entered ID is incorrect');\n";
    echo "window.location='index.html?loginFailed=true&reason=wrongID'";
echo "</script>";
exit;

1 Comment

@BHappy did you notice that my method doesn't modify the entire array just to find existence of one possible value? Whether you move the green tick is up to you, but using a loop, strpos(), and an early exit is the most direct way.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.