title: hangman "word" in php
body: <p>I am trying to do something similar to hangman where when you guess a letter, it replaces an underscore with what the letter is. I have come up with a way, but it seems very inefficient and I am wondering if there is a better way. Here is what I have - </p>
  
  <pre><code>&lt;?
  $word = 'ball';
  $lettersGuessed = array('b','a');
  
  echo str_replace( $lettersGuessed , '_' , $word ); // __ll
  
  echo '&lt;br&gt;';
  
  $wordArray = str_split ( $word );
  
  foreach ( $wordArray as $letterCheck )
  {
  
      if ( in_array( $letterCheck, $lettersGuessed ) )
      {
          $finalWord .= $letterCheck;
      } else {
          $finalWord .= '_';
      }
  
  }
  
  echo $finalWord; // ba__
  ?&gt;
  </code></pre>
  
  <p><code>str_replace</code> does the opposite of what I want. I want what the value of <code>$finalWord</code> is without having to go through a loop to get the result I desire.</p>