nicholaswmin
nicholaswmin

Reputation: 22949

Simple IF/ELSEIF not comparing properly

I'm new to PHP and this was asked before but the answers just won't cut it in my scenario.

I have this piece of code:

  if ($node->field_available_for_payment[LANGUAGE_NONE][0]['value']==0){
  }  elseif($node->field_available_for_payment[LANGUAGE_NONE][0]['value']==1){
     $status="awaitingapproval";
  }  elseif (3===3){
     $status="paid";
  } elseif ($node->field_shipped[LANGUAGE_NONE][0]['value']==1){
     $status="shipped";
  } 

 var_dump($status);

I get back value awaitingapproval (the first if/elseif evaluate to TRUE).

However shouldn't I be getting back 'paid' instead since the 3===3 comparison evaluates to TRUE?

as well?

All the other S.0 answers regarding this type of questions mention the '=' operator vs '==' which is correct in my code.

Upvotes: 0

Views: 42

Answers (1)

John Conde
John Conde

Reputation: 219824

Control structures like if/else stop executing once a truthy statement is reached. Since the first block is true the other blocks are never evaluated.

If that first block should ever fail, (i.e. evaluate to false) then your second statement will always evaluate to true and the code will be executed.

Upvotes: 5

Related Questions