Search code examples
phpforeachreturn

How can I store the data in the foreach loop in PHP? (Hangman Game)


I have a problem with the store the data in foreach loop.!

It's Hangman game and when the user inputs the character to check the answer,

echo "Please enter a guess: ";
$userChar = stream_get_line(STDIN,1024,PHP_EOL);
//guessChar($hangman,$userChar); 
//Display a masked version of the name according to the attributes in the Array
$inputArray [] =$userChar;
foreach(str_split($hangman)as $char)
{

    if(in_array($char,$inputArray,true))
    {
    echo $char;
    }
    else
    {
    echo "*";
    }

}

But in there, I want to store the string what console shows to compare the hangman's answers to finish game or not

enter image description here

I want to get that blocked string to pass the function which is used for check the answer.


Solution

  • You already have all the guesses saved up. Just output them.

    echo implode('', $inputArray);