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 trialTim Statler
Courses Plus Student 12,745 PointsPing Pong - PHP. My code is the same as the teachers on the video but it's coming back showing only player 2.
In Ping Pong | PHP and Control Structures, my code is the same as the teachers but when I view it on a web browser, I'm not getting the same result as her. Should show player1 and player2, but just showing player2. Code below
<?php $player1 = 0; $player2 = 0; $round = 0;
//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 players scores each round $round++; echo "<h2>Round $round</h2\n"; if (rand(0,1)) { $player1++; } else { $player2++; } echo "Player1 = $player1<br />\n"; echo "Player2 = $player2<br />\n"; } echo "<h1>"; if ($player1 > $player2) { echo "Player1"; } else { echo "Player2"; } echo " is the winner after $round rounds!</h1>\n";
?>
1 Answer
Steven Parker
231,269 PointsIt may not be exactly "the same" as in the video. It's hard to read unformatted code, but it looks like there's an incomplete tag in one of the echo statements:
echo "<h2>Round $round</h2\n"; // <-- missing ">" after last "h2"
For future postings, use the instructions for code formatting in the Markdown Cheatsheet pop-up below the "Add an Answer" area. Or watch this video on code formatting.
Tim Statler
Courses Plus Student 12,745 PointsTim Statler
Courses Plus Student 12,745 Points1. You were right, and that was the issue.
2. I didn't realize my example code would be unformatted like that, my apologies. That said, bravo for finding the error in that mess!
Thanks for your help!!