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 PHP Arrays and Control Structures PHP Loops Ping Pong

nathan goel
nathan goel
7,410 Points

I guess I don't really understand && and ||...help please

inside my while condition I state $player1 < 11 && $player2 < 11...I first tried || instead of && because as I understand it I need player1 score to be at least 11 or player2 score to be at least 11...so why do I get an endless loop when using || instead of &&?

<?php
$player1 = 0;
$player2 = 0;
$round = 0;

while($player1 < 11 && $player2 < 11 || abs($player1 - $player2) < 2) {
    echo "<h1><b>Round: " . $round . "</b></h1><br>";
    echo "Player 1 score: " . $score1 = rand(0,1) . "<br>";
    if($score1 == 1) {
        echo "Player 2 score: " . $score2 = 0 . "<br>" . "<br>";    
    } else {
        echo "Player 2 score: " . $score2 = 1 . "<br>" . "<br>";
    };

    if ($score1 > $score2) {
        $player1++;
    } else {
        $player2++;
    };

    echo "Player 1 total score: " . $player1 . "<br>";
    echo "Player 2 total score: " . $player2 . "<br>" . "<br>";
    $round++;
};

if($player1 == 11) {
    echo "Player 1 is the winner with a score of: $player1"; 
} else {
    echo "Player 2 is the winner with a score of: $player2"; 
};

//To Win
//player must reach a score of 11
//player must be a minimum of 2 points higher than opponent
//WHILE those conditions are NOT met, a single player will receive 1 point at the end of each round.
?>

Moderator edited: Markdown added so that code renders properly in the forums.

1 Answer

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there, nathan goel ! Looks like you're doing pretty well. I would note, however, that your code differs quite a bit from the video and in those echo statements you are actually reassigning the value of the scores. Ultimately, it works out, but be careful about that :smiley:

As for the OR and AND (|| &&), an || will evaluate to true if any of those conditions evaluate to true. Now, this is highly unlikely, but imagine this scenario: imagine that the first player rolls a one eleven times in a row. Their score would be 11 while player2 would be 0. But that should be game over, right?

However, if you have the || instead of the &&, it will keep on going until player2 also has 11 points. Remember, if either one of them haven't reached 11 it's going to keep going. You need the AND here to tell it to keep going as long as both of them are under eleven or the difference is less than 2 between them. So if player1 has 8 points and player2 has 9 points, keep going. However, if player2 has 11 points and player1 has 0, we should definitely stop.

Hope this helps! :sparkles: