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 trialyahya alhalki
1,149 Pointswhy 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) )
2 Answers
Piotr Manczak
Front End Web Development Techdegree Graduate 29,277 PointsWe want to keep it as a separate condition. We want to evaluate it separately.
yahya alhalki
1,149 Pointswhat is the use of that. i tried to remove the parenthesis and they both execute the same way
Damir Paulić
6,591 PointsWell, 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.
Michael Cook
Full Stack JavaScript Techdegree Graduate 28,975 PointsMichael Cook
Full Stack JavaScript Techdegree Graduate 28,975 PointsYou need to explain your question to get an answer. The title of your question is vague.