Softnux
Softnux

Reputation: 2730

in_array is both true and false

Odd problem here. I'm writing a simple Hangman game in PHP, and while checking if the letters in the current word matches those guessed, I run in to a problem.

The code is as follows:


    $letters = str_split($words[$_SESSION['hangman']['current_word']]);
    foreach($letters as $letter)
    {
        if(in_array($letter, $_SESSION['hangman']['guessed']) == True);
        {
            echo "true for ", $letter , "
"; } }

the if-statement will always evaluate to true, even if i change the line to


if(in_array($letter, $_SESSION['hangman']['guessed']) == False);

Upvotes: 0

Views: 149

Answers (1)

Pekka
Pekka

Reputation: 449415

It's because of the semicolon at the end of your line:

if(in_array($letter, $_SESSION['hangman']['guessed']) == False);

the semicolon ends the condition; the code after it will always be executed.

Remove the semicolon to make it work as intended.

Upvotes: 6

Related Questions