Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Ping Pong Logical Operators

//continue the game while the absolute value is less than ; while (abs($player1 - $player2) < 2 || $player1 < 11 && $player2 < 11) Why do you use the logical operators where you do. In Alena's game, the score starts out with an absolute value of 5. So wouldn't the game be over if you use the || or logical operator where you do? If the absolute value needs to be greater than 2 for the loop to stop or the players must have a score of greater than 11 to win then I think the game should have ended before it began. Shouldn't the first logical operator be &&? And why is the second logical operator && shouldn't it be || since we only want one of the players to have a score greater than 11 to win? I am so confused by this lesson.

2 Answers

Niki Molnar
Niki Molnar
25,698 Points

I'm not sure which of the ping-pong lessons you are referring to but the correct IF statement should be:

if( abs( $player1 - $player2 ) >= 2 && ( $player1 >= 11 || $player2 >= 11 ) ) {
    // Someone won
} else {
    // Game continues
}

So yes, you're right about the middle && operator

I am referring to the lesson in the Beginner PHP track, in the PHP Arrays and Control Structures Course, in the PHP Loops section, the video lesson titled Ping Pong. Alena starts off by stating the conditions that need to be present in order for there to be a winner for a "ping-pong game". She says that for a player to win the game at least one player must have a score of 11 and that the winner must have at least 2 points higher than the loser. She said that both of these things need to be true for there to be a winner. But in the "while" statement that she writes for the game she uses the || to imply that one or the other needs to be true. And she uses a && to imply that both people need to have a score higher or equal to 11. I would send you a picture of this code but I am sorry I do not know how.

This is what she writes:

$player1= 0;
$player2= 5;
$round = 0;

//To Win
//player must reach a score of 11
//player must be a minimum of 2 points higher than opponent
while (abs($player1 - $player2)<2 || ($player1 < 11 && $player2 < 11)) {
  //NOT WINNER
 //randomly increment one of the player's score each round
  $round++;
  echo "<h2>Round $round</h2>\n";
  if (rand(0,1)) {
      $player1++;
  }   else {
      $player2++;
  }
  echo "Player 1 = $player1<br />\n";
  echo "Player 2 = $player2<br />\n";
}
echo "<h1>";
if($player1 > $player2) {
  echo "Player1";
} else {
  echo "Player2";
}
echo " is the winner after $round rounds!</h1>\n";
?>```

I do not understand her use of logical operators...

I think I know the answer to my question of why the certain logical operators where used.

while (abs($player1 - $player2) < 2 || $player1 < 11 && $player2 < 11)

While either of the conditions are true then the loop will continue to run. Even thought the first condition is false (bc the abs starting value is more than 2) the second condition is true so the while loop will continue to run untill both conditions are false.