How can the issue of prompting for input even after the word has been correctly guessed be resolved in PHP?

The issue of prompting for input even after the word has been correctly guessed can be resolved by adding a condition to check if the word has been guessed before prompting for input again. This can be done by setting a flag when the word is correctly guessed and using this flag to control the input prompt.

<?php
$word = "apple";
$guessed = false;

if(isset($_POST['guess'])){
    $guess = $_POST['guess'];
    
    if($guess == $word){
        echo "Congratulations! You guessed the word.";
        $guessed = true;
    } else {
        echo "Try again!";
    }
}

if(!$guessed){
    echo "<form method='post'>
            <label for='guess'>Guess the word:</label>
            <input type='text' name='guess' id='guess'>
            <button type='submit'>Submit</button>
          </form>";
}
?>