'If condition is meet but is ignored in PHP code

I have the following code to validate if a username is entered:

 if (!isset($_POST['username']) ) {
    
    header("location: index.php");
    $error_message = "Please enter your Username and Password!";
    exit();
 }

If I don't enter any value in the username field, the condition turns out false and would execute the code inside the if statement and similar situation when I enter a username!

Any idea why this is happening?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source