PHP if condition with strings

if statementPHPstring

I am trying to write would be a simple if condition.

function genderMatch($consumerid1, $consumerid2)
    {
    $gender1=getGender($consumerid1);
    $gender2=getGender($consumerid2);
    echo $gender1;
    echo $gender2;
    if($gender1=$gender2)   
      echo 1;
      return 1;
    else
       echo 0;
       return 0;
}

The output of the getGender function is either a M or F. However, no matter what I do gender1 and gender2 are returned as the same. For example I get this output: MF1

I am currently at a loss, any suggestions?

Best Answer

if ($gender1 = $gender2)

assigns the value of $gender2 to $gender1 and proceeds if the result (i.e. the value of $gender2) evaluates to true (every non-empty string does). You want

if ($gender1 == $gender2)

By the way, the whole function could be written shorter, like this:

function genderMatch($cid1, $cid2) {
  return getGender($cid1) == getGender($cid2);
}