I am making a simple hangman application and I have a php file and a separate .txt file containing words, one on each line.
I want the $ word variable to remain constant even after the page has been refreshed, since I planned to use GET or POST to enter the user.
In the code example below, I want $ word to stay the same after submitting the form. I believe this is a simple question about moving the code to another place, but I cannot figure out what help for this PHP noob would be appreciated!
wordsEn1.txt:
cat dog
functions.php:
<?php function choose_word($words) { return trim($words[array_rand($words)]); } ?>
hangman.php:
<?php include('functions.php'); $handle = fopen('wordsEn1.txt', 'r'); $words = array(); while(!feof($handle)) { $words[] = trim(fgets($handle)); } $word = choose_word($words); echo($word); echo('<form><input type="text" name="guess"></form>'); ?>
source share