How can the PHP code be modified to only display the "Incorrect Password" message if the user has attempted to log in and the credentials are incorrect?

The issue can be solved by adding a condition to check if the user has attempted to log in and if the credentials are incorrect before displaying the "Incorrect Password" message. This can be achieved by setting a flag when the login form is submitted and the credentials are checked. If the flag is set and the credentials are incorrect, then display the message.

<?php
// Check if the form is submitted
if(isset($_POST['submit'])){
    // Check login credentials
    $username = "admin";
    $password = "password";

    if($_POST['username'] == $username && $_POST['password'] == $password){
        echo "Login successful!";
    } else {
        // Display message only if login attempted and incorrect
        echo "Incorrect Password";
    }
}
?>