Welcome to the 7th PHP Coding puzzle !

Game Title: “Hangman”

Objective: The goal of the game is to guess a hidden word one letter at a time.


  1. The game begins by selecting a random word from a pre-defined list.
  2. The player is shown a series of underscores that represent each letter of the word.
  3. The player is given a certain number of attempts to guess the word. Let’s say the player has 6 attempts.
  4. On each turn, the player enters a letter.
  5. If the letter is in the word, the corresponding underscore is replaced with the letter.
  6. If the letter is not in the word, the game displays a part of a hangman figure. The figure is built up one part at a time as the player makes incorrect guesses.
  7. The game continues until the player either correctly guesses the word or the hangman figure is complete.

Coding Challenge:

Write a PHP function that implements the “Hangman” game. The function should take an array of words as an argument and select a random word from the array. The function should display a series of underscores that represent each letter of the word. The function should then prompt the player for a letter and continue the game as described above. The function should return either the string “You win!” if the player correctly guesses the word, or the string “You lose!” if the hangman figure is complete.

Here’s a function signature to get you started:

function hangman($words) {
  // Your code here

The solution is already available in the next page 😉 Feel free to share with us your solution here in comments or Github.


Please enter your comment!
Please enter your name here