Working through a tutorial and I can't figure out how to make "Both can't win!" display. Can anyone explain this to me. Seems like it should be simple enough. Thanks.

$contestant_one='best teeth';
$contestant_two='best ears';

if($contestant_one xor $contestant_two){
print("Both can't win!");
}

else{
print('Only one winner!');
}

    thank you. wonder why they wrote the code like this. I know that it's just an example, but.......

      <?php
      
      $contestant_one='best teeth';
      $contestant_two='best ears';
      
      if($contestant_one xor $contestant_two){
      	print("We have just one winner.");
      }
      
      else{
      	print("Both can't win!");
      }
      
      echo "\r\n----------\r\n";
      
      $contestant_one='best teeth';
      $contestant_two='';
      
      if($contestant_one xor $contestant_two){
      	print("We have just one winner.");
      }
      
      else{
      	print("Both can't win!");
      }
      

      Output:

      Both can't win!
      ----------
      We have just one winner.
      

      PS: Note that the "both can't win" message would also display if both variables were empty, so the message could be misleading in this case.

        well, I don't see how xor would be very useful in this case. How would you be able to use xor to test two variables where you would only have one winner. Am I missing the point? The above code is taken right out of the tutorial.

          ok. This makes sense:

          $contestant_one="winner";
          $contestant_two="loser";

          if($contestant_one=="winner" xor $contestant_two=="winner"){
          print("I won!");
          }

          else{
          print('Only one winner!');
          }

            The original code is rubbish; without context I can't say whether that reflects on the quality of the tutorial.

              Write a Reply...