Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

PHP PHP Arrays and Control Structures PHP Loops Ping Pong

yahya alhalki
yahya alhalki
1,149 Points

why do we have to put parentheses round ($player1 < 11 && $player2 < 11) )?

//My Code

<?php


$player1 = 1;
$player2 = 2;
$round = 0;
$difference = abs($player1 - $player2) < 2;

while($difference|| ($player1 < 11 && $player2 < 11) ){

  $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";
}


?>


//why do we have to put parentheses round  ($player1 < 11 && $player2 < 11) )

You need to explain your question to get an answer. The title of your question is vague.

2 Answers

We want to keep it as a separate condition. We want to evaluate it separately.

yahya alhalki
yahya alhalki
1,149 Points

what is the use of that. i tried to remove the parenthesis and they both execute the same way

Damir Paulić
Damir Paulić
6,591 Points

Well, because of the operator precedence, parentheses in this case is not needed. "&&" is executed before "||". https://www.php.net/manual/en/language.operators.precedence.php

Personally, I would still use parentheses because it could get overwhelming to think about precedence while still figuring the logical part.