Q:

Hangman PHP , but my word changes each time my submit is pressed

I am trying to make a hangman game and I have an array with some words in it. It successfully picks a random word but the word changes every time the submit button is pressed, how do I stop this? My code looks like this
<html>
<head>
<title> Hangman </title>
</head>
<body>
<h1> Hangman <h1>   
<?php
session_start(); 
$maxAttempts = 6; 
$letters = array('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o',
'p','q','r','s','t','u','v','w','x','y','z');
?>
<form name="lettersubmit" method="post" action="Hangman.php" > 
<input name="letterguess" type= "text" value="">
<input name="submit" type="submit" value="Guess a Letter!"><br>
<p> Guesses Remaining <p>
<input name="triesRemaining" type="text" value="<?php print("$maxAttempts"); ?>">
</form>
<?php
$letterguess = $_POST["letterguess"];

if($letterguess= $word){
    echo ("correct");
}
if(!isset($_POST["submit"])){
    $words  = array (
    "giants",
    "triangle",
    "particle",
    "birdhouse",
    "minimum",
    "flood",

);
}
if(isset($_POST["submit"])){
    $maxAttempts--;
}

$word = $words[array_rand($words)];
    echo $words[array_rand($words)];
?>
</body>
</html>

If you can see any other mistakes feel free to point them out. Thanks Zac

A:

It is because you send a new request to hangman.php (which I assume is this page you are showing us). This reloads the whole page, with the exception of your code setting the words array. The code determining the random word to use runs regardless of the new request being a submit.
If you add that to the code block that should only run initially it will only pick a word when it starts. 
if(!isset($_POST["submit"])){
    $words  = array (
    "giants",
    "triangle",
    "particle",
    "birdhouse",
    "minimum",
    "flood",

    $word = $words[array_rand($words)];
);

Once you have done that you should store it somewhere, the session as commented in the is one way. A hidden field in your form is another (less pretty) way to store the value, then you can read from the form data when receiving a submit. 

