rblerk
rblerk

Reputation: 17

if elseif statement in PHP

Both if and elseif statement are working at the same time.

  while($answerResult = mysql_fetch_assoc($answers))
  {
    if($reportResult != 0 && $this->CheckAnswer($questionId,$answerResult['id'],$reportResult))
    {
       echo '<input type="radio" name="question['.$questionId.']" value="'.$answerResult['id'].'" checked/>'.$answerResult['name'].'aaa&nbsp;&nbsp';
    }
    elseif($sampleResult != 0 && $this->CheckAnswer($questionId,$answerResult['id'],$sampleResult))
    {
       echo '<input type="radio" name="question['.$questionId.']" value="'.$answerResult['id'].'" checked/>'.$answerResult['name'].'hee&nbsp;&nbsp';
    }
    else
    {
       echo '<input type="radio" name="question['.$questionId.']" value="'.$answerResult['id'].'" />'.$answerResult['name'].'&nbsp;&nbsp';
    }
  }

I'm trying to do if my first statement is true then don't look other conditions but if my first condition is false then check elseif condition.

However, my elseif condition is working even though first if condition is true.

I put 'hee' and 'aaa' at the end of the 'echos' to see which one is printing.

The result:

First, if statement is working and printing with 'aaa' then looking at the elseif statement and checked this and print 'hee'.

So, I don't know what i am missing and can't find any solution, Is there any suggestion?

Upvotes: 0

Views: 212

Answers (1)

Manuel
Manuel

Reputation: 483

The if, elseif, and else in one statement are exclusive, so this shouldn't happen.

The whole statement is in a while loop ... are you sure it's not the if being executed in the first iteration and the elseif being executed in the second iteration?

Upvotes: 3

Related Questions