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

Carlos Zuluaga
Carlos Zuluaga
21,184 Points

The value echo for the final rounds taken always brings 0 (initial value)

Hello, So when I echo " Is the Winner after $rounds Rounds! </h1>" the value of $rounds = 0. It's taking the value I gave to the variable initially.

This is my code:

<?php $player1 = 0; $player2 = 5; $rounds = 0;

while (abs($player1-$player2) < 2 || ($player1< 11 && $player2 <11)){ $round++; echo "<h2>This is Round $round</h2><br>"; if (rand(0,1)){ $player1++; } else {$player2++; } echo "player 1 = $player1<br>"; echo "player 2 = $player2<br>"; } echo "<h1>"; if ($player1 > $player2){ echo "Player 1"; }else{ echo "Player 2"; } echo " Is the Winner after $rounds Rounds! </h1>" ?>

2 Answers

Brian Workman
Brian Workman
10,519 Points

You have $rounds = 0; but then in the while loop you use $round++; (notice the missing s)

I think that might be your problem.