Hello All,
I have an IF statement that consists of two separate function calls passing values to two variables. Obviously if neither value is 'FALSE' then the code block is executed:
<?php
   class MyValidater {
      static function validateString($string) {
         if (preg_match("/[A-Za-z]+/", $string)) {
            return $string;
         } else {
            return false;
         }
      }
   }
   $firstName = "Daniel";
   $surname = "Simmons";
   // Dodgy IF statement
   if ($first = MyValidater::validateString($firstName) && $second = MyValidater::validateString($surname)) {
      print("Success: $first $second");
   } else {
      print("Fail: $first $second");
   }
?>
As you can see both the $first and $second variables should contain the values held in $firstName and $surname after successfully being validated by the Static method validateString.
However, the values of the two variables end up: $first = '1' and $second = "Simmons".
The '1' should be "Daniel" but for some reason $first is being passed the value '1' or TRUE. If you swap the two assignment statements over so that $second is evaluated first, you end up with the opposite outcome as before. $second = '1' and $first = "Daniel"
Can anyone explain why the String value "Daniel" being returned from the class method is being changed into the int '1' for the first part of the conditional statement only? I have had a quick look though the PHP documentation but cannot find an explanation.
For the moment the workaround is to change the return value from the static method to be true/false and then make $first = $firstName, etc... upon success. But this involves more code and I would rather find out why this way does not work.
Thanks,
Dan